Error creating a node

A

Allan Bruce

I am making my own linked list implementation just to get some basic
knowledge of slightly more advanced C++. I have the class below, but I get
an error saying "error C2629: unexpected 'Node ('" which is on the line for
the overlaoded constructor Node(char xiType)
Can anybody tell me where I am going wrong?
Thanks
Allan


#include <iostream>

class Node
{
public:
Node();
Node(char xiType);
virtual ~Node();

void SetNextLink(Node *xiNode){NextNode = xiNode);
void SetPrevLink(Node *xiNode){PrevNode = xiNode);
Node *GetPrevLink(){return PrevNode;}
Node *GetNextLink(){return NextNode;}
void SetData(void *xiData){Data = xiData;}
void *GetData(){return Data;}
private:
Node *NextNode;
Node *PrevNode;
void *Data;
bool Tail;
bool Head;
};
 
R

Ron Natalie

Allan Bruce said:
I am making my own linked list implementation just to get some basic
knowledge of slightly more advanced C++. I have the class below, but I get
an error saying "error C2629: unexpected 'Node ('" which is on the line for
the overlaoded constructor Node(char xiType)
Can anybody tell me where I am going wrong?

Are you sure the previous line really has a semicolon at the end of it?
 
A

Allan Bruce

Ron Natalie said:
Are you sure the previous line really has a semicolon at the end of it?

There are no other classes before this one, but they do all have semi-colons
at the end of the class definitions.
Allan
 
R

red floyd

Allan said:
There are no other classes before this one, but they do all have semi-colons
at the end of the class definitions.
Allan

No, Ron meant are you absolutely sure that you have a semicolon after the default constructor, and not a colon?
i.e. did you retype, or did you cut&paste?
 
A

Allan Bruce

Allan Bruce said:
I am making my own linked list implementation just to get some basic
knowledge of slightly more advanced C++. I have the class below, but I get
an error saying "error C2629: unexpected 'Node ('" which is on the line for
the overlaoded constructor Node(char xiType)
Can anybody tell me where I am going wrong?
Thanks
Allan


#include <iostream>

class Node
{
public:
Node();
Node(char xiType);
virtual ~Node();

void SetNextLink(Node *xiNode){NextNode = xiNode);
void SetPrevLink(Node *xiNode){PrevNode = xiNode);
Node *GetPrevLink(){return PrevNode;}
Node *GetNextLink(){return NextNode;}
void SetData(void *xiData){Data = xiData;}
void *GetData(){return Data;}
private:
Node *NextNode;
Node *PrevNode;
void *Data;
bool Tail;
bool Head;
};

I think the problem is in my methods, as I am trying to return a pointer to
a class called Node, but during the definition of Node itself - how do I
this in C++?
Thanks
Allan
 
A

Allan Bruce

red floyd said:
No, Ron meant are you absolutely sure that you have a semicolon after the
default constructor, and not a colon?
i.e. did you retype, or did you cut&paste?


I did cut and paste but missed out the inline constructors. I found that
there was an error there as I was using a type BYTE without including the
necessary header.
My main problem is in the fact that I want to return a pointer to a class
that I am defining, if you know what I mean.
Allan
 
A

Allan Bruce

Adam Fineman said:
'{NextNode = xiNode);'

should be: '{NextNode = xiNode;}'

Note that you've paired a right paren to a left curly brace. Also,
you've put the semicolon after the right paren, when it should be at the
end of the statement.


Same mistakes on this line.


The rest is fine, although I'm not crazy about the pointers to void. I
suspect that either you or your mentor is a refugee from C. ;-)

Seriously, pointers to void are usually not appropriate in C++ code,
unless you need to interface with existing code written in C.

Thanks.
I have originally came from a C background hence the void pointers. What
would be the best way to implement this in C++?
Also, I have a problem with the code in my methods, as I am returning a Node
* during the defintion of class Node. My compiler doesnt like it. I guess
it is similar to C where I would have to use 'struct Node *' whilst in the
definition of the struct, but I dont know how to do it for classes.
Allan
 
A

Adam Fineman

Allan said:
I am making my own linked list implementation just to get some basic
knowledge of slightly more advanced C++. I have the class below, but I get
an error saying "error C2629: unexpected 'Node ('" which is on the line for
the overlaoded constructor Node(char xiType)
Can anybody tell me where I am going wrong?
Thanks
Allan


#include <iostream>

class Node
{
public:
Node();
Node(char xiType);
virtual ~Node();

void SetNextLink(Node *xiNode){NextNode = xiNode);
'{NextNode = xiNode);'

should be: '{NextNode = xiNode;}'

Note that you've paired a right paren to a left curly brace. Also,
you've put the semicolon after the right paren, when it should be at the
end of the statement.
void SetPrevLink(Node *xiNode){PrevNode = xiNode);

Same mistakes on this line.
Node *GetPrevLink(){return PrevNode;}
Node *GetNextLink(){return NextNode;}
void SetData(void *xiData){Data = xiData;}
void *GetData(){return Data;}
private:
Node *NextNode;
Node *PrevNode;
void *Data;
bool Tail;
bool Head;
};

The rest is fine, although I'm not crazy about the pointers to void. I
suspect that either you or your mentor is a refugee from C. ;-)

Seriously, pointers to void are usually not appropriate in C++ code,
unless you need to interface with existing code written in C.

- Adam
 
A

Allan Bruce

Adam Fineman said:
'{NextNode = xiNode);'

should be: '{NextNode = xiNode;}'

Note that you've paired a right paren to a left curly brace. Also,
you've put the semicolon after the right paren, when it should be at the
end of the statement.


Same mistakes on this line.


The rest is fine, although I'm not crazy about the pointers to void. I
suspect that either you or your mentor is a refugee from C. ;-)

Seriously, pointers to void are usually not appropriate in C++ code,
unless you need to interface with existing code written in C.

Thanks.
I have originally came from a C background hence the void pointers. What
would be the best way to implement this in C++?
Allan
 
A

Adam Fineman

Allan said:
Thanks.
I have originally came from a C background hence the void pointers. What
would be the best way to implement this in C++?
Allan

If you're making a truely generic container, then you should use a template:

template<typename data_t>
class my_list
{
// ...

private:
data_t* d_data;
// ...
};

There are, of course, many alternatives. You could use some type of
smart pointer to data_t instead of a native pointer.

The template lets you store any type in this container. If you know
beforehand what types will be stored, you may opt to represent data_t as
a class or hierarchy of classes.

The advantage of these approaches over using void* is that you have some
type safety.

You should check out the FAQ for this newsgroup for some good ideas:
http://www.parashift.com/c++-faq-lite/

Also, you could check out the book reviews at http://www.accu.org/.

HTH,

Adam
 
A

Allan Bruce

If you're making a truely generic container, then you should use a template:

template<typename data_t>
class my_list
{
// ...

private:
data_t* d_data;
// ...
};

There are, of course, many alternatives. You could use some type of
smart pointer to data_t instead of a native pointer.

The template lets you store any type in this container. If you know
beforehand what types will be stored, you may opt to represent data_t as
a class or hierarchy of classes.

The advantage of these approaches over using void* is that you have some
type safety.

You should check out the FAQ for this newsgroup for some good ideas:
http://www.parashift.com/c++-faq-lite/

Also, you could check out the book reviews at http://www.accu.org/.

HTH,

Adam

Thanks, I have had a look at the FAQ, but I am still having trouble with the
argument list. I posted the problems about it in a post subject "Using
Class Templates". I have another question though. Am I correct in thinking
I have to specify in the argument list, all types I wish to store in my
list? If so, is this not rather restricitve? I mean, if I want to store a
type of a specific class, I would have to add this class to the argument
list?
Thanks
Allan
 
A

Adam Fineman

Allan Bruce wrote:
I have another question though. Am I correct in thinking
I have to specify in the argument list, all types I wish to store in my
list? If so, is this not rather restricitve? I mean, if I want to store a
type of a specific class, I would have to add this class to the argument
list?

I'm not sure what you mean by "argument list." Class templates have to
be specialized when you create an object of that class. If you want
examples of this, have a look at std::list, std::vector, etc. For example:

std::list<int> loi;
loi.push_back(1);
loi.push_back(2);

class some_type { /* ... */ };

std::vector<some_type> lost;
some_type st(/* ... */);
lost.push_back(st);

etc.

Have a look at some good C++ standard library documentation for more
information.

- Adam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top