我正在尝试创建一个结构,其中包含一个类型为相同结构的向量.但是,当我构建时,错误表明我错过了';' 在'>'出现之前.我不确定编译器是否甚至认为向量是一个东西:/并且我已经包含在我的代码中.这是我到目前为止:
#include <vector>
typedef struct tnode
{
int data;
vector<tnode> children;
GLfloat x; //x coordinate of node
GLfloat y; //y coordinate of node
} tnode;
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激!!
您的代码正在调用未定义的行为,因为标准容器(例如vector不能包含不tnode完整的类型)在结构定义中是不完整的类型.根据C++ 11标准,17.6.4.8p2:
在以下情况下,效果未定义:[...]如果在实例化模板组件时将不完整类型(3.9)用作模板参数,除非特别允许该组件.
所述Boost.Container库提供可替换的容器(包括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
};
Run Code Online (Sandbox Code Playgroud)