0

I made this c++ Linked List project with CLion IDE and CMake. When I try to compile and run it, this error appears:

CMakeFiles\LinkedList.dir/objects.a(main.cpp.obj): In function `main':
D:/C++/LinkedList/main.cpp:10: undefined reference to `LinkedList<int>::LinkedList()'
D:/C++/LinkedList/main.cpp:11: undefined reference to `LinkedList<int>::append(int)'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\LinkedList.dir\build.make:124: LinkedList.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:86: CMakeFiles/LinkedList.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:93: CMakeFiles/LinkedList.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:123: LinkedList] Error 2

Initially I tought that the problem was CMake, but in Visual Studio i got a similar error:

error LNK2019: unresolved external symbol "public: bool __thiscall LinkedList<int>::append(int)" (?append@?$LinkedList@H@@QAE_NH@Z) referenced in function _main

Why on earth is this happening? I declared (or, most probably, I THINK I did) all functions correctly, but anyway I provided the code below.

This is my main.cpp:

#include <iostream>
#include "LinkedList.h"
#include "LinkedListNode.h"
using namespace std;


int main() {
    LinkedList<int> *ls = new LinkedList<int>(); //undefined reference at this line
    bool x = ls->append(10); //undefined reference at this line
    return 0;
}

This is LinkedList.h:

#include "LinkedListNode.h"

#ifndef LINKEDLIST_LINKEDLIST_H
#define LINKEDLIST_LINKEDLIST_H

template<typename T>
class LinkedList {
private:
   LinkedListNode<T>* first;
   int length;
public:
    LinkedList();
    bool append(LinkedListNode<T>*);
    bool append(T);
    bool insert(int,LinkedListNode<T>*);
    bool insert(int,T);
    bool remove(int);
    bool pop();
    LinkedListNode<T>* getFirst();
    LinkedListNode<T>* getLast();
    int getLength();
    LinkedListNode<T>* get(int);
};


#endif //LINKEDLIST_LINKEDLIST_H

This is an extract of LinkedList.cpp with only the append and constructor functions:


template<typename T>
LinkedList<T>::LinkedList() {

}

template<typename T>
bool LinkedList<T>::append(T val) {
    LinkedListNode<T> *node = new LinkedListNode<T>(val);
    if (!this->first){
        this->first = node;
        this->length++;
        return true;
    }
    LinkedListNode<T> *last = this->getLast();
    last->setNext(node);
    this->length++;
    return true;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.20)
project(LinkedList)

set(CMAKE_CXX_STANDARD 14)

add_executable(LinkedList main.cpp LinkedList.cpp LinkedList.h LinkedListNode.cpp LinkedListNode.h)

If someone can help me, I will really appreciate it.

0 Answers0