0

我有一个定义如下的类:

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

我已经审查了好几次,但无法弄清楚这个错误的根源是什么......

4

1 回答 1

1

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

于 2013-05-02T15:05:36.007 回答