我是 C++ 的新手,我正在尝试编译 my code .我使用的命令是 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
有人知道这是怎么回事吗?非常感谢!
最佳答案
您似乎在 header 中声明了一个模板并在 C++ 文件中定义了它。这行不通。如果不在 header 中定义模板,则需要在 C++ 文件中显式实例化它,例如,使用
template class gcs::Channel<int>;
在所有方法的定义之后。
关于c++ - 编译 C++ : undefined reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19897298/