(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{
private:
int i;
node *next;
public:
node();
node(int);
~node();
void setNode(int);
};
node.cpp contains the definition of constructor/destructor in the following format. It also "#include "node.h""
node::node()
{
node::setNode(0);
cout<<"default constructor"<<endl;
node *next = new node;
}
node::node(int value)
{
node::setNode(value);
cout<<"value constructor"<<endl;
node *next = new node;
}
node::~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".