简体   繁体   English

链接器错误:找不到ld符号

[英]Linker error: ld symbols not found

I have a class defined as follows: 我有一个定义如下的类:

// grid_search.h
template<typename Element, typename Solution, typename Oracle>
class grid_search : public OptimizerInterface<Element, Solution, Oracle> {
private:
    // ...
public:
    virtual Solution search(const SpaceInterface<Element>& space, Oracle oracle);
};

and I implement it in grid_search.cc . 我在grid_search.cc实现了它。 Now I use it like this: 现在,我像这样使用它:

// needed extra definitions 
typedef double (*oracle_f)(vector<int>&);
class TestSpace : public SpaceInterface<int> { /* ... */ }
static double f(vector<int>& x) { return 0.0; } // what ever f

// setup a search space and run the grid search
TestSpace space(/* ... */);
GridSearch<int, vector<int>, oracle_f> *optimizer = new GridSearch<int, vector<int>, oracle_f>();
optimizer->search(space, f);

Then the linker error is (note that everything compiles and the grid_search.cc is found fine): 然后是链接器错误(请注意,所有内容都会编译,并且可以找到grid_search.cc):

Undefined symbols for architecture x86_64:
"GridSearch<int, std::vector<int, std::allocator<int> >, double (*)(std::vector<int, std::allocator<int> >&)>::search(SpaceInterface<int> const&, double (*)(std::vector<int, std::allocator<int> >&))", referenced from:
    vtable for GridSearch<int, std::vector<int, std::allocator<int> >, double (*)(std::vector<int, std::allocator<int> >&)> in grid_search.cc.o

I have reviewed it several times but can't figure out what the root of this error is ... 我已经对其进行了多次审查,但无法弄清楚此错误的根源是什么...

模板函数定义通常需要在头文件中,而不是.cc文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM