473,569 Members | 2,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initializating members with new

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
3 1237

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

"Gaijinco" <ga******@gmail .com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2055
by: Carlos Ribeiro | last post by:
I thought about this problem over the weekend, after long hours of hacking some metaclasses to allow me to express some real case data structures as Python classes. I think that this is something with potential to be useful, but I would like to hear more opinions first. If this is deemed to be useful, I *may* try to write a PEP for it. This...
8
3938
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access private members of nesting class. template <typename T> class Container { // NO friendship given to any other public: class ContainerIterator;
3
3587
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming non-standard C++ or if the problem lies elsewhere. To summarize static const class members are not being accessed properly when accessed from a...
13
7275
by: John | last post by:
In the course of an assignment, I learned the hard way that I shouldn't try to free a malloc'd member of a malloc'd structure after having freed that structure (i.e., free( structure ); free( structure->bufferspace ) ). My question is, if I free just the structure, will the (e.g.) bufferspace be freed implicitly, or do I have to (as I...
5
1634
by: radek jedrasiak | last post by:
Hi all, this is about an idea for a language feature. Any feedback welcome. To start with: This is about a language feature, which would allow to filter the set of available members of a type within a defined scope, it is also about defining "views" for types. It's about controlling what features / feature sets of external libraries...
2
9594
by: Dave Veeneman | last post by:
Is is legal to declare abstract members in non-abstract classes? How about non-abstract members in abstract classes? I am writing a base class with three derived classes. The base class will define the behavior for most, but not all of its members. The derived classes will define the behavior for the remaining members (the undefined...
5
14015
by: Genboy | last post by:
My "VIS" Website, which is a C# site created in VS.NET, Framework 1.1, is no longer compiling for me via the command line. As I have done 600 times in the last year and a half, I can compile to VIS.DLL via Visual Studio, with no problems: ------ Rebuild All started: Project: VIS, Configuration: Debug .NET ------ Preparing resources......
10
25721
by: Abelardo Vacca | last post by:
Hi, The title sums up the question pretty much. I would like to access all private members of a class including the private members of its base classes.( I already have the ReflectionPermission ) Is there a way to get this information ? Thnaks in advance
11
3810
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of...
14
2614
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming Language, but there isn't a detail and complete description on all the class members, aren't they important to class composing? Could you explain the...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.