0

I'm trying to declare an array inside a typedef struct like this:

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

7
  • In C++, brackets in array initialization are placed after the identifer both on left and right-hand sides, all the time. Node arr[25] Commented Apr 7, 2017 at 2:30
  • 1
    Do you really want to allocate 25 Nodes in a Node? Commented Apr 7, 2017 at 2:31
  • I tried that before but when I do that, it says "incompatible type is not allowed" underneath the arr and underneath the second Node it says "the generated default constructor for "Node" cannot be used in an initializer for its own data member" Commented Apr 7, 2017 at 2:32
  • 1
    @Chris Then each of 25 nodes also have their own 25 nodes? Commented Apr 7, 2017 at 2:34
  • 1
    @Chris You'd better reconsider the design, at least the code you showed won't work, it tempts to cause an infinite construction of Node. And Node is not complete type until the end of the class definnition, so you can't do new Node[25] inside the class definition. Commented Apr 7, 2017 at 2:40

1 Answer 1

1

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;


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


i think you wanted something like this

struct Node {
    static const int arr_size = 25;
    Node* arr[arr_size];
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.