0

Hi there, I'm new to the forum so please be kind :)

I've created a class called node that has a standard constructor and an overload that uses an int as an argument.

class node
{
public:
	node(void);
	node(int inputs);
...
};

I can create an dynamic array of nodes with

node *nodes;
node = new nodes[number];

which works very nicely. But now I want to setup the nodes using the overloaded constructor. I tried

node = new nodes(width)[number];

but that just created a whole load of errors. I realise I could make another function in the node class that could make the changes the overload would've done but I want this to be as neat as possible. Any help would be greatly appreciated.
Thank you, Tom

4
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by Duki
0

Hey Tom, you can do this two ways. The easiest way would be to use a vector instead of an array:

vector<Node> Tom(5, Tom(width));  //5 Tom objects calling Tom(width)

The other way, if you want to (a) specify the parameters for each object or (b) continue using arrays:

Node Tom[5] = ( Tom(1), Tom(2), Tom(3), Tom(4), Tom(5) );

Hope this helps

Edited by Duki: n/a

0

To do this dynamically you need to initialize each object individually, like this:

node nodeArray = new node[number];
for (int i = 0; i < number; ++i)
  nodeArray[i] = node(5);

Duki's right, the easiest thing to do is use a vector. In general, if you can use std::vector, std::string, or std::auto_ptr to do something, do it. Allocating memory using new opens up a lot of problems, the least of which is calling the right constructors.

Also Duki, I think you switched from Nodes to Toms on accident. And arrays initializer lists use curly braces, not parentheses.

0

Oh, hey, I did not know auto_ptr was being deprecated. That is good to know. Although I never really use them in the first place...

0

>Also Duki, I think you switched from Nodes to Toms on accident. And arrays initializer lists use curly braces, not parentheses.

Sure did - thanks.

0

Thanks for the suggestions. I've finally been able to get over a huge stumbling block with all your help.

0

Good to hear - don't forget to mark your post as solved in case others may need to reference it.

[and for self esteem reasons]

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.