gpt4 book ai didi

c++ - 如何在同一结构中声明结构的 vector ?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:19:35 27 4
gpt4 key购买 nike

我正在尝试创建一个结构,其中包含一个类型为同一结构的 vector 。但是,当我构建时,错误表明我缺少“;”在“>”出现之前。我不确定编译器是否甚至将 vector 识别为一个东西:/并且我已经包含在我的代码中。这是我目前所拥有的:

#include <vector>

typedef struct tnode
{
int data;
vector<tnode> children;
GLfloat x; //x coordinate of node
GLfloat y; //y coordinate of node
} tnode;

任何帮助将不胜感激!!

最佳答案

您的代码正在调用未定义的行为,因为 vector 等标准容器不能包含不完整的类型,而 tnode 是结构定义中的不完整类型。根据 C++11 标准,17.6.4.8p2:

the effects are undefined in the following cases: [...] if an incomplete type (3.9) is used as a template argument when instantiating a template component, unless specifically allowed for that component.

Boost.Container library提供可包含不完整类型的替代容器(包括 vector)。递归数据类型(例如您想要的那种)作为此用例给出。

以下内容适用于 Boost.Container:

#include <boost/container/vector.hpp>
struct tnode
{
int data;

//tnode is an incomplete type here, but that's allowed with Boost.Container
boost::container::vector<tnode> children;

GLfloat x; //x coordinate of node
GLfloat y; //y coordinate of node
};

关于c++ - 如何在同一结构中声明结构的 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17325473/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com