cxf54 cxf54 - 2 months ago 14
C++ Question

relationship between node.h/node.cpp/nodelist.cpp

(old questions answered, i need to compile two cpp files together.
New questions on infinite priting of "default constructor" is posted.)

I have three files.

node.h defines node class with constructor, destructor declaration but i want to put the actual definition into node.cpp

class node{
int i;
node *next;
void setNode(int);

node.cpp contains the definition of constructor/destructor in the following format. It also "#include "node.h""

cout<<"default constructor"<<endl;
node *next = new node;
node::node(int value)
cout<<"value constructor"<<endl;
node *next = new node;
cout<<"default destructor"<<endl;
delete next;
node::setNode(int value){
i = value;

in the nodelist.cpp, it is my main func for now.
it includes "node.h".
But the compilation seems can't find the constructor and destructor

g++ nodelist.cpp
/tmp/cc6PoQNR.o: In function `main':
nodelist.cpp:(.text+0x11): undefined reference to `node::node()'
nodelist.cpp:(.text+0x22): undefined reference to `node::~node()'
collect2: error: ld returned 1 exit status

Can anyone explain what is the issue?

If possible, maybe elaborate more on some knowledge behind this issue that i lack of.

Thanks guys. I need to compile node.cpp and nodelist.cpp together.

The next question is:

in my nodelist.cpp, i just typed

int main(){
node x;
return 0;

but it is printing infinite number of "default constructor".

Answer Source

node *next = new node; this line inside default constructor will call another default constructor for node to be set into next, which will call another default constructor, which will... etc

set next to nullptr - you do not want to allocate more than one node at a time