Sign in to follow this  
Vish

[C++] template base class problems

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Is your class split into two different files? Is there a header (.h) file and the implementation? Because, with template classes, the implementation must be in the same file as the declaration of the class. So you must move the implementation code of the class, which you did not post, to the same file as the declarations. So you would move them to the header file.

This does make it difficult if you are making a very large project, especially if you have a lot of code in the implementation, but it is necessary when using templates. Here, have a look at this article that explains the reason for this: Templates
Go to the bottom of that tutorial and it explains why you must keep the implementation and header of a template class in the same file.

I hope that helped with your problem!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this