c++ - 编译 C++ : undefined reference

标签 c++ gcc compilation compiler-errors g++

我是 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/

相关文章:

C:在没有 main() 的情况下编译某些对象(用于共享库)失败?

iOS 构建在编译时失败,出现问题 "failed to find a suitable device for the type SimDeviceType"

c++ - 两次相同的for循环 : one compiles, 另一个没有

c++ - ld : symbol(s) not found for architecture x86_64 error on array of structs

c++ - 在 C++ 中制作对象列表

c++ - 对 `krb5_kt_free_entry' 的 undefined reference

c++ - 段错误。地址 0x0 未被堆栈、malloc 或(最近)释放

linux - 在linux中编译用户空间代码

编译具有有限库访问权限的程序

c++ - 两个模板类互为成员组成