struct tree{ int data; tree * next; tree(int numLeaves) { cout << "tree object created\n"; next = new tree[numLeaves]; } tree() { }};
When a tree object is created with an integer argument, am I correct in assuming this will create an array of pointers to 'tree' objects of a size equal to the argument.
The following code is giving compile-time errors:
int main(){ tree test(2); test.data = 1; test.next[0] = new tree(3); return 0;}
It says there's an invalid conversion from tree* to int.
I'm confused since I used similar code to make a statically linked tree by omitting both constructors and initializing the pointer array as follows:
tree *next[2];
So what am I doing wrong in initializing this array of pointers dynamically? Or is my error in how I'm trying to access it?
edit: and my guess is that i'm looking at the constructor as creating an array of pointers to trees when in fact it's actually creating the trees themselves.