[英]Linker error: ld symbols not found
我有一個定義如下的類:
// 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
我已經對其進行了多次審查,但無法弄清楚此錯誤的根源是什么...
模板函數定義通常需要在頭文件中,而不是.cc文件中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.