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

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

Member Avatar for embooglement

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.

>>std::auto_ptr

Thats being deprecated. So I would avoid that. I would rather use boost smart pointers

Member Avatar for embooglement

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...

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

Sure did - thanks.

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

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]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.