Reputation: 253
I have a pretty specific data structure issue that I cannot figure out. For my assignment, I must have a dynamically allocated array of structures in my private section of my header. So, up to this point, in my header file, I have
struct node{
int name;
node *next;};
In my private, I have
node *adj;
which is, at least to my knowledge, how you would set up having the array.
Then, in my .cpp file, I have
adj = new node*[];
This is throwing a bunch of errors. But, when I have
node *adj[n];
in my cpp and nothing in my header, it works. My questions is how do I have an array in my private section of my header, but dynamically allocate the space in my .cpp?
Upvotes: 1
Views: 170
Reputation: 2739
You have defined
node *adj;
which is a pointer to a node, or an array of nodes. If you want an array of pointers to nodes, you should declare
node **adj;
which is a pointer to a pointer to a node, or an array of pointers to nodes. In the first case, you allocate the array using
adj = new node[n];
which defines an array of nodes. and in the second you use
adj = new node*[n];
which defines an array of node pointers.
adj = new node*[];
should not make sense as it does not tell the compiler how big to make the array.
Upvotes: 2