c++ tree within an array

-1

I'm trying to make an array that hold splay trees. I tried:

SplayTree<Node> splayArray[];

Then initializing it with:

splayArray[10];

However, when I try to insert using:

splayArray[0].insert(nodeObject);

It doesn't work, I keep getting this error:

0xC0000005: Access violation reading location 0x00000018

Note that everything works fine if I just make one splay tree and inserting nodes in there.

c++
arrays
tree
asked on Stack Overflow Nov 2, 2014 by user2963379

1 Answer

1

Here

splayArray[10];

You're not initializing, but accessing.

You should write this instead :

SplayTree<Node> splayArray[10]; // Here goes the size of the array
splayArray[0].insert(nodeObject);

Arrays have to know their size at declaration, or to calculate them at initialization (for instance int array[] = { 42 }; // array of size 1).

EDIT:

Regarding your comment, place this in your header file:

extern SplayTree<Node> splayArray[10];

Then place this in the global scope of your .cpp file:

SplayTree<Node> splayArray[10];

And then this inside a function, it is up to you to call it at the right time:

splayArray[0].insert(nodeObject);
answered on Stack Overflow Nov 2, 2014 by Chnossos • edited Nov 2, 2014 by Chnossos

User contributions licensed under CC BY-SA 3.0