对c+;中已定义函数的未定义引用+;? 我对C++非常陌生,所以我需要一些帮助。代码如下:

对c+;中已定义函数的未定义引用+;? 我对C++非常陌生,所以我需要一些帮助。代码如下:,c++,compilation,compiler-errors,g++,C++,Compilation,Compiler Errors,G++,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{ pri

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;
};
我在网上看过,但我真的不明白哪里出了问题。我也尝试过使用代码块进行构建,但是没有用


谢谢大家!

您的
.cpp
文件应如下所示:

#include "RootNode.h"

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

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

它应该包括rootnode.h,不是吗?当然。我将添加它您现在不需要
类RootNode
定义-它来自RootNode。记住,如果您想定义自定义构造函数和析构函数,您必须在RootNode.h中的类定义中插入原型
#include "RootNode.h"

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

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