c++ - 模板方法上的 gcc 链接错误

标签 c++ gcc linker-errors

我遇到了这个链接器错误,但在克服它时遇到了一些麻烦。

o/foo/bar.cc.o: In function 'foo::bar()': bar.cc:(.text+0x728): undefined reference to 'rf<PlayerInfo> Util::getClosestToGlobal<PlayerInfo>(std::vector<rf<bats::PlayerInfo>, std::allocator<rf<PlayerInfo> > >, Eigen::Matrix<double, 3, 1, 2, 3, 1>)'

Util.h文件定义:

template <class ObjectClass>
static rf<ObjectClass> getClosestToGlobal(
    std::vector<rf<ObjectClass> > objects, Eigen::Vector3d targetPosGlobal);

Util.cpp定义:

template <class ObjectClass>
rf<ObjectClass> Util::getClosestToGlobal(
    std::vector<rf<ObjectClass> > objects, Eigen::Vector3d targetPosGlobal)
{
    // ...
}

我知道我的 cpp 文件编译成功,因为它按预期重新创建了 .o 文件。

我是否在此处提供了足够的信息以供更有经验的人识别问题?如果没有,我还应该去哪里找?

最佳答案

模板类/方法定义应该可用 in the header file .

关于c++ - 模板方法上的 gcc 链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10572093/

相关文章:

c++ - 这是 ->> 一个旧的运算符还是一个错字/错误?

c++ - 冲突的 boost 版本

c - gcc -O2 优化错误的行为,这是一个错误吗?

c++ - 如何从 C++ 代码调用 Matlab?

c++ - ICU ustdio.h 函数 : Undefined symbols on Mac OS when building with CMake

c++ - 仿函数与模板参数

c++ - theSize/2 是什么意思 percolateDown(i) 函数?

c++ - 如果 "variable or field"被声明为无效,这意味着什么?错误信息帮助

c++ - 在哪里可以找到 C++ 中 operator new 的确切实现?

C++:调试 C++ 模板链接器错误