C ++:对(模板构造函数)

时间:2018-01-15 06:55:16

标签: c++ c++11

我是一名尝试学习C ++的Java开发人员。我正在使用CLion并尝试使用模板类编译一个非常基本的程序。这是错误:

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

所有代码:

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 (自动生成)

cmake_minimum_required(VERSION 3.9)
project(collections2)

set(CMAKE_CXX_STANDARD 11)

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

0 个答案:

没有答案