在C++中的结构定义中使用结构本身
Using the struct itself in the definition of struct in C++
我想在C++中实现一个简单的树结构,比如:
struct node{
....
node* parent;
node[]* children;
....
};
但是编译器报告了一个错误(CLang++和G++)
error: expected unqualified-id before '[' token
node[]* child;
^
error: expected ',' or '...' before '*' token
node(node[]* c): : child = c; {}
^
像这个
顺便说一句,我正在为一些c++11功能使用-std=c++11标志
任何帮助都会通知
结构的大小是编译时常数。如果您给出一个size to be determined later
的数组,则这是一个错误。给定一定的大小(对于二进制树的情况下的ex 2)或使用pointer to node *
存储数组或指针(子级),或使用一些内置容器(对于ex std::vector,std::array)
node[]* children;
不是合法的C++语法。如果最大在编译时,子级的数量是已知的,您可以写:
node* children[maxChildren];
否则:
std::vector<node*> children;
会成功的。如果maxChildren
较大或可变,则可能还是要使用此表单。另一方面,如果maxChildren
是2,您可能只需要声明两个指针:
node* leftChild;
node* rightChild;
相关文章:
- 我的超类中的模板问题与结构定义
- 为私有结构定义双参数运算符重载
- 可视化C++中的结构定义错误
- 如何在结构定义中组合__attribute__((dllexport))和[[nodiscard]]?
- COM 互操作结构定义与内存布局不匹配
- C 在标题文件中创建具有结构定义的结构的队列
- 如何将结构定义为结构的成员
- 在这个节点定义中,为什么我们在结构定义之后使用"Node"?
- 不同模块中的ambigus结构定义
- 有条件的C/C 结构定义
- C++ 初始化结构定义中的向量
- 字符串声明中的结构定义中的字符串声明
- 使用结构定义函数
- 为具有许多数据成员的结构定义 == 和<
- enableif结构定义和默认模板参数
- C 使用模板为结构定义别名
- C/C++ 中的套接字编程 - 接收由 typedef 结构定义的消息
- 为C++结构定义显式析构函数如何影响调用约定
- 当结构定义在头文件中时,如何在main()中创建结构数组
- C结构定义标准