0

I'm attempting to create a node class for use in a later to be coded linked list class, but while testing the node I've been encountering an error, "undefined reference to 'node::node()'". I've found similar errors on other questions asked but none of them have given an answer that applies here. This is my main.cpp file,

#include "node.h"

using namespace std;

int main()
{
    node obj;
    int j = 1000;
    obj.setAnd(j);
    cout << obj.getAnd();
    return 0;
}

My node.h file,

#ifndef NODE_H
#define NODE_H
class node
{
    private:
        int operand;
        char oper;
        node *next;
    public:
        node();
        node(int);
        node(char);
        int getAnd(){return operand;}
        int getOr(){return oper;}
        node* getNext(){return next;}

        void setAnd(int a){operand=a;}
        void setOr(char o){oper=o;}
        void setNext(node* newNext){next=newNext;}



};

#endif // NODE_H

And my node.cpp file.

#include "node.h"

node::node()
{
    //ctor
    /*int operand= 0;
    char oper= 'a';
    next = NULL;*/
}
node::node(int an)
{
    operand=an;
}
node::node(char or)
{
    oper=or;

}

node::~node()
{
    //dtor
}

I can't seem to see anything wrong here, so I'm wondering if it's how my compiler is running.

Edit: I created a new project and pasted all the files into the new project and that fixed it.

  • 1
    "undefined reference" means the compiler can't find the code - in this case the node.cpp file. How are you telling the compiler where your files are and which ones to compile? Do you have a makefile or are you using a GUI of some kind. Show me the command the compiler gets. – Jerry Jeremiah Nov 18 '19 at 00:47
  • 1
    (1) `or` is a c++ keyword, do not use it as an identifier name. (2) You cannot define a destructor that is implicitly declared. After fixing the above errors your code should compile, given that you **used the commands correctly**. – ph3rin Nov 18 '19 at 00:50
  • Also, since your parameterless constructor and the destructor are empty, you could instead declare them in the class like `node() = default;` and delete the do-nothing implementions from the .cpp file. – Scott Hutchinson Nov 18 '19 at 00:55
  • @JerryJeremiah I'm using the basic Code::Blocks compiler, I'll admit I have little to no experience with changing my compiler commands. And, given that the only flag checked is -g, I will assume something is wrong on that end. – Tobias Fizzlewig Nov 18 '19 at 01:02
  • The important thing is how do you tell the compiler you want to compile both main.cpp and node.cpp to make the executable. It won't be i nthe compiler options - it will be a GUI thing like having them both showing up in the same list or under the same folder. I don't use Code::Blocks so I am not sure what it looks like. – Jerry Jeremiah Nov 18 '19 at 01:06
  • Does this help: https://stackoverflow.com/questions/5971206/codeblocks-how-to-compile-multiple-source-files It looks like exactly what you are asking. Here is the answer to that one: Go to the left panel that says projects, and right-click on node.cpp file. Select properties, then go to build. Check the boxes under the heading Belongs in Targets: "Debug" and "Release" – Jerry Jeremiah Nov 18 '19 at 01:07
  • You probably did not even compile `node.cpp` – M.M Nov 18 '19 at 01:09
  • I think the link I gave is more of a duplicate than the one linked to because mine is exactly the same question about the same GUI. – Jerry Jeremiah Nov 18 '19 at 01:19
  • Maybe this is a good duplicate: https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – Jerry Jeremiah Nov 18 '19 at 20:15

0 Answers0