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

时间:2013-05-02 15:03:35

标签: c++

我的课程定义如下:

// 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);
};

我在grid_search.cc中实现它。现在我像这样使用它:

// 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);

然后链接器错误是(注意一切都编译好了,找到了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

我已多次审核过,但无法弄清楚这个错误的根源是什么......

1 个答案:

答案 0 :(得分:1)

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

相关问题