我对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
有谁知道这里发生了什么?非常感谢!
答案 0 :(得分:6)
似乎您在标头中声明了一个模板,并在C ++文件中定义了该模板。这不行。如果未在标头中定义模板,则需要在C ++文件中显式实例化,例如:使用
template class gcs::Channel<int>;
定义所有方法之后。