0

I'm a Java developer trying to learn C++. I'm using CLion and trying to compile a very basic program using template classes. Here's the error:

C:\Users\Tony\.CLion2017.3\system\cygwin_cmake\bin\cmake.exe --build C:\Users\Tony\workspace\cpp\collections2\cmake-build-debug --target collections2 -- -j 2
Scanning dependencies of target collections2
[ 33%] Building CXX object CMakeFiles/collections2.dir/main.cpp.o
[ 66%] Linking CXX executable collections2.exe
CMakeFiles/collections2.dir/main.cpp.o: In function `main':
/cygdrive/c/Users/Tony/workspace/cpp/collections2/main.cpp:7: undefined reference to `LinkedList<int>::LinkedList()'
/cygdrive/c/Users/Tony/workspace/cpp/collections2/main.cpp:7:(.text+0x3d): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `LinkedList<int>::LinkedList()'
/cygdrive/c/Users/Tony/workspace/cpp/collections2/main.cpp:7: undefined reference to `LinkedList<int>::~LinkedList()'
/cygdrive/c/Users/Tony/workspace/cpp/collections2/main.cpp:7:(.text+0x4e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `LinkedList<int>::~LinkedList()'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/collections2.dir/build.make:121: collections2.exe] Error 1
make[2]: *** [CMakeFiles/Makefile2:68: CMakeFiles/collections2.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:80: CMakeFiles/collections2.dir/rule] Error 2
make: *** [Makefile:118: collections2] Error 2

And all the code:

LinkedList.h

#ifndef COLLECTIONS2_LINKEDLIST_H
#define COLLECTIONS2_LINKEDLIST_H

template <typename E>
class LinkedList {
public:
    LinkedList<E>();
    ~LinkedList<E>();

    void add(const E &e);
};


#endif //COLLECTIONS2_LINKEDLIST_H

LinkedList.cpp

#include "LinkedList.h"

template <typename E>
LinkedList<E>::LinkedList() {
}

template <typename E>
LinkedList<E>::~LinkedList() {
}

template <typename E>
void LinkedList<E>::add(const E &e) {
}

main.cpp

#include "LinkedList.h"

int main() {
    LinkedList<int> list;
    return 0;
}

CMakeLists.txt (auto-generated)

cmake_minimum_required(VERSION 3.9)
project(collections2)

set(CMAKE_CXX_STANDARD 11)

add_executable(collections2 LinkedList.cpp LinkedList.h main.cpp)
AutonomousApps
  • 4,229
  • 4
  • 32
  • 42
  • 2
    Templates are not types, and thus their translation units work a bit differently. In your code you have made the assumption that the header with your declarations has *visible* definitions. it doesn't, you must put your definitions either in the same header, or as is often the case, in another file (*.tpl, *.hpp, *imp, *.ipl) which is then included **by the header**. Because the implementation isn't associated and seen by the template definition, the references are undefined. – Ælex Jan 15 '18 at 13:04
  • After my question was marked as duplicate, I checked those answers and tried the simplest approach of moving my implementation into the header file and it worked. Obviously I have a lot to learn about C++, and my Java-esque assumptions about how code works simply don't translate. – AutonomousApps Jan 15 '18 at 18:19

0 Answers0