I was constructing a simple base class to implement an abstract interface for an A* Pathfinder. And I thought I might as well use templates (Don't have much experience with templates).
So the base class is something like this -
template <typename State, typename Action>
class PathFinder {
public :
typedef std::pair<State, Action> PSA;
typedef State state_type;
typedef Action action_type;
PathFinder();
PathFinder(const State & start_state);
virtual ~PathFinder();
virtual int h(const State & s) = 0;
virtual int g(const State & ps, const Action & a, const State & ns) = 0;
virtual bool goal_test(const State &s) = 0;
virtual bool map_test(const State &s) = 0;
virtual std::list<PSA> sucessor_function(const State &s) =0;
void set_start_state(const State &s);
bool find_path();
std::list<PSA> & get_path() { return path; }
private :
//Whatever
All the non virtual member functions have been defined in a separate source file. Then I proceeded to derive a class from this "PathFinder" class.
class MapPF : public PathFinder<Tile, Action> {
//Whatever
public :
MapPF();
~MapPF();
typedef std::pair<Tile, Action> PSA;
virtual int h(const Tile & s);
virtual int g(const Tile & ps, const Action & a, const Tile & ns);
virtual bool goal_test(const Tile &s);
virtual bool map_test(const Tile &s);
virtual std::list<PSA> sucessor_function(const Tile &s);
void generate_map();
void render();
SDL_Surface* get_surface() { return surface; }
};
Here Tile refers to a simple struct and Action refers to a enum.
I defined all the functions (including the virtual ones) in a separate file. After which I wrote a simple program to test this class out (using SDL for rendering, in case you haven't inferred that already).
After compiling everything (and fixing usual typos and other mundane errors) via g++ (Ubuntu) I seem to be getting the following errors -
/tmp/ccfoeiGF.o: In function `MapPF::generate_map()':
MapPF.cpp:(.text+0x3c8): undefined reference to `PathFinder<Tile, Action>::set_start_state(Tile const&)'
/tmp/ccfoeiGF.o: In function `MapPF::~MapPF()':
MapPF.cpp:(.text+0x555): undefined reference to `PathFinder<Tile, Action>::~PathFinder()'
MapPF.cpp:(.text+0x56e): undefined reference to `PathFinder<Tile, Action>::~PathFinder()'
/tmp/ccfoeiGF.o: In function `MapPF::~MapPF()':
MapPF.cpp:(.text+0x5c9): undefined reference to `PathFinder<Tile, Action>::~PathFinder()'
.
.
.
Basically every time I try to use one of PathFinder's functions via MapPF errors are produced.
Now I remembered reading something about inheritance not working when a templatized Base class is used in 'Effective C++' so I re-read the relevant portion and added the following code to MapPF -
public :
using PathFinder<Tile, Action>::set_start_state;
using PathFinder<Tile, Action>::find_path;
using PathFinder<Tile, Action>::get_path;
Unfortunately even this doesn't seem to help.
Any suggestions anyone ?
EDIT : If anyone wants me to post the implementation. Just Ask !! :-)
EDIT : Here is the command I'm using for compiling
g++ -L/usr/lib -lSDL -o main main.cpp PathFinder.cpp MapPF.cpp -Wall
[Edited by - Vish on July 28, 2008 12:54:03 PM]