[英]Declaring an array inside typedef struct
I'm trying to declare an array inside a typedef struct like this: 我试图在这样的typedef结构中声明一个数组:
typedef struct Node {
Node[] arr = new Node[25];
};
But I am getting an error saying "expected an identifier" and that arr "expected a ';'. What am I doing wrong? Thank you 但是我收到一个错误消息,说“期望的标识符”,而arr“期望的是';'。我在做什么错呢?谢谢
you can act like this 你可以这样
struct Node {
static const int arr_size = 25;
Node* arr;
Node() { arr = new Node[arr_size]; }
~Node() { delete[] arr; }
};
you re not allowed initialzie non const int varizbles inside the class; 您不允许在班级内使用initialzie non const int varizbles;
and do you understand, that creating a node variable will call stack overflow ? 而且您了解吗,创建节点变量将调用堆栈溢出? Each node contains 25 nodes where each node contains 25 nodes ... etc 每个节点包含25个节点,其中每个节点包含25个节点...等等
i think you wanted something like this 我想你想要这样的东西
struct Node {
static const int arr_size = 25;
Node* arr[arr_size];
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.