2

Im very new to c++, so I will need some help for this. Here is the code:

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;
};

I get this error when I try to compile with g++:

$ 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

I have looked online, but I really dont understand what goes wrong. I have also tried to build with CodeBlocks, but its of no use.

Thank you!

1 Answer 1

3

Your .cpp file should look something like this:

#include "RootNode.h"

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

void RootNode::setNodeId(int i)
{
// stuff
}
Sign up to request clarification or add additional context in comments.

4 Comments

And it should probably #include rootnode.h shouldn't it?
Definitely. I will add it
You don't need the class RootNode definition now - that's coming from RootNode.h
And remember that if you want to define custom constructor and destructor you have to insert prototypes in your class definition in RootNode.h

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.