0

I'm doing some linked list practices in C++ and created a simple class for the single linked list. When I try to include the header-file in the main program however I get an undefined reference error. If I include the .cpp file however it works as I want it to.

It's been a while since I last coded in C++ and I can't for the love of me figure out what's wrong. Some help would be deeply appreciated. I'm using Windows Termnial with git-bash interface and g++ -std=c++11. The code is included below!

//The main file .cpp
#include "SingleNode.h"

int main() {
        SingleNode* tail = new SingleNode(2);
        SingleNode* head = new SingleNode(1, tail);
        head->print();
        return 0;
}
//SingleNode.h
#ifndef SINGLENODE_H
#define SINGLENODE_H

        class SingleNode {
                public:
                        int val;
                        SingleNode* next;
                        SingleNode();
                        SingleNode(int x);
                        SingleNode(int x, SingleNode* next);

                        void print();
        };
#endif
//SingleNode.cpp
#include "SingleNode.h"
#include <iostream>

using namespace std;
        SingleNode::SingleNode() : val(0), next(nullptr) {}
        SingleNode::SingleNode(int x) : val(x), next(nullptr) {}
        SingleNode::SingleNode(int x, SingleNode* next) : val(x), next(next) {}

        void SingleNode::print() {
                if (this->next != nullptr) {
                        cout<<this->val<<"->";
                        this->next->print();
                } else {
                        cout<<this->val<<"->"<<"null"<<endl;
                }
        }

When run:

$ g++ -std=c++11 LinkedList.cpp -o LinkedList.exe
C:\AppData\Local\Temp\ccEbmcRt.o:LinkedList.cpp:(.text+0x30): undefined reference to `SingleNode::SingleNode(int)'
C:\AppData\Local\Temp\ccEbmcRt.o:LinkedList.cpp:(.text+0x59): undefined reference to `SingleNode::SingleNode(int, SingleNode*)'
C:\AppData\Local\Temp\ccEbmcRt.o:LinkedList.cpp:(.text+0x69): undefined reference to `SingleNode::print()'
collect2.exe: error: ld returned 1 exit status

If I instead #include "SingleNode.cpp" it works fine.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 1
    Fyi, [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – WhozCraig Feb 08 '22 at 09:54
  • Thank you! Looks like just a kind of read I needed! – yoyoakokimon Feb 08 '22 at 10:04

1 Answers1

3

You include the header file, so prototypes are available, and the compiler does not complain. The linker needs to find the source associated with those functions. Using g++ -std=c++11 LinkedList.cpp -o LinkedList.exe to compile means that you're only using the main file source and not the other file that contains the linked list implementation.

The solution is to also pass the SingleNode.cpp file to the compiler. Hence:

g++ -std=c++11 LinkedList.cpp SingleNode.cpp -o LinkedList.exe and include the SingleNode.h file in LinkedList.cpp.

The #include directive performs textual substitution, which means that when you include the .cpp file(which, in turn, includes the header file) you have one final translation unit that contains the required source of both source files.

Also see: Why should I not include cpp files and instead use a header?

Tortellini Teusday
  • 1,335
  • 1
  • 12
  • 21