2016-04-25 7 views
2

Ich bin sehr neu in C++, also brauche ich etwas Hilfe dafür. Hier ist der Code:undefinierter Verweis auf eine definierte Funktion in C++?

RootNode.h

#ifndef ROOTNODE_H 
#define ROOTNODE_H 


class RootNode 
{ 
    public: 

     int getNodeId(); 
     void setNodeId(int i); 

    protected: 
    private: 
     int node_id; 
}; 

#endif // ROOTNODE_H 

RootNode.cpp

class RootNode{ 
    private: 
     int node_id; 

RootNode() 
{ 
    //ctor 
    this->setNodeId(0); 
} 

void setNodeId(int i){ 
    node_id = i; 
} 

int getNodeId(){ 
    return node_id; 
} 

~RootNode() 
{ 
    //dtor 
} 

}; 

main.cpp

#include <stdio.h> 
#include <iostream> 
#include "LeafNode.h" 
#include "DecisionNode.h" 
#include "RootNode.h" 
using namespace std; 


int main(int argc, const char* argv[]){ 
     RootNode rootNode; 
     cout<<rootNode.getNodeId(); 




return 0; 
}; 

Ich bekomme diese Fehlermeldung, wenn ich versuche, mit g ++ kompiliert :

$ g++ RootNode.cpp main.cpp 
/tmp/ccylUcLS.o: In function `main': 
main.cpp:(.text+0x17): undefined reference to `RootNode::getNodeId()' 
collect2: error: ld returned 1 exit status 

Ich habe online gesucht, aber ich verstehe wirklich nicht, was schief geht. Ich habe auch versucht, mit CodeBlocks zu bauen, aber es ist nutzlos.

Vielen Dank!

Antwort

3

Ihre .cpp Datei sollte wie folgt aussehen:

#include "RootNode.h" 

int RootNode::getNodeId() 
{ 
// stuff 
} 

void RootNode::setNodeId(int i) 
{ 
// stuff 
} 
+0

Und es sollte wohl #include rootnode.h sollte es nicht? – kfsone

+0

Definitiv. Ich werde es hinzufügen –

+1

Sie brauchen nicht die 'class RootNode' Definition jetzt - das kommt von RootNode.h – kfsone

Verwandte Themen