编译C++:未定义的引用

Der*_*ang 0 c++ gcc compiler-errors compilation g++

我是C++的新手,我正在尝试编译我的代码.我正在使用的命令是g++ -o main --std=c++11 main.cpp channel.cpp.但是我收到以下错误消息:

/tmp/ccLuJs81.o: In function `main':
main.cpp:(.text+0x26): undefined reference to `gsc::Channel<int>::Channel()'
main.cpp:(.text+0x3a): undefined reference to `gsc::Channel<int>::put(int)'
main.cpp:(.text+0x4e): undefined reference to `gsc::Channel<int>::get(bool)'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

有谁知道这里发生了什么?非常感谢!

Die*_*ühl 6

似乎您在标头中声明了一个模板,并在C++文件中对其进行了定义.这不行.如果未在标头中定义模板,则需要在C++文件中显式实例化它,例如,使用

template class gcs::Channel<int>;
Run Code Online (Sandbox Code Playgroud)

在定义了所有方法之后.