SkyWalker
SkyWalker

Reputation: 14317

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. 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):

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

Upvotes: 0

Views: 123

Answers (1)

John Zwinck
John Zwinck

Reputation: 249552

Template function definitions usually need to be in the header file, not a .cc file.

Upvotes: 1

Related Questions