By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,376 Members | 1,557 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,376 IT Pros & Developers. It's quick & easy.

Initializating members with new

P: n/a
If I have this:

struct node{

int data;
node *left, *right;

};

It's possible to in-code say something like:

nodo *head = new node (1,NULL,NULL);

The compilers says "no overloaded function takes 3 parameters" but I
don't know what it means.
Or is a way to declare default parameters, like everytime that a new
node is created it should have data = 0, left = right = NULL?

Nov 3 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Gaijinco wrote:
If I have this:

struct node{

int data;
node *left, *right;

};

It's possible to in-code say something like:

nodo *head = new node (1,NULL,NULL);\
No.

If you were not using new, you could initialize a node like this:

node my_node = { 1, NULL, NULL };
The compilers says "no overloaded function takes 3 parameters" but I
don't know what it means.
the statement "node* head = new node( 1, NULL, NULL);" tries to create
a node object and call a constructor that takes three parameters. Like
so:

struct node
{
node( int data_, node* left_, node* right_ )
: data(data_), left( left_ ), right( right_ ) { }

int data;
node *left, *right;
};
Or is a way to declare default parameters, like everytime that a new
node is created it should have data = 0, left = right = NULL?


See above...
You could also add default values to the constructor so that "new node"
works correctly as well. Like so:

struct node
{
node( int data_ = 0, node* left_ = 0, node* right_ = 0 )
: data(data_), left( left_ ), right( right_ ) { }

int data;
node *left, *right;
};

And finally, note that if you're looking to implement a linked list in
C++, you may save yourself some work and use std::list instead:

http://www.sgi.com/tech/stl/List.html

Cheers,
Andre

Nov 3 '05 #2

P: n/a

"Gaijinco" <ga******@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
If I have this:

struct node{

int data;
node *left, *right;

};

It's possible to in-code say something like:

nodo *head = new node (1,NULL,NULL);
Not it's not, since you've not defined a constructor
which takes three arguments of the necessary types.

The compilers says "no overloaded function takes 3 parameters" but I
don't know what it means.
It means that you asked it to invoke a constructor
which takes threee arguments, but there isn't one.
Or is a way to declare default parameters, like everytime that a new
node is created it should have data = 0, left = right = NULL?

struct node
{
node(int d = 0, node *lt = 0, node *rt = 0)
: data(d), left(lt), right(rt)
{}

int data;
node *left, *right;
};

Which C++ book(s) are you reading that don't explain the
fundamental concept of 'constructor'?

You should also read about 'public' and 'private', and 'class'
vs. 'struct'.

-Mike
Nov 3 '05 #3

P: n/a
Gaijinco wrote:
If I have this:

struct node{

int data;
node *left, *right;

};

It's possible to in-code say something like:

nodo *head = new node (1,NULL,NULL);
With the above declaration of 'node' - no.
The compilers says "no overloaded function takes 3 parameters" but I
don't know what it means.
'node' has no constructors that take 3 parameters.

However, it has an implicitly declared copy-constructor, which means that you
can achieve what you want in the following manner

const node init = { 1, NULL, NULL }; // or just '= { 1 };'
node* head = new node(init);

But this is not very elegant since it requires an extra object. Better declare
and define the appropriate constructor, as others suggested, if you don't mind
losing the POD-ness of your 'node' type.
Or is a way to declare default parameters, like everytime that a new
node is created it should have data = 0, left = right = NULL?


All-zeros is a different story. As long as you object is a POD, you can use a
'()' initializer to achieve zero-initialization

node* head = new node();
// Here all fields of 'head' are set to zero
assert(head->data == 0 && head->left == NULL && head->right == NULL);

However in this case you have to remember to use the '()' initializer every time
you create an object. If you don't like this, you might be better off explicitly
declaring a default constructor for 'node' and making it to set all fields to zero.

--
Best regards,
Andrey Tarasevich

Nov 4 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.