2 Replies - 261 Views - Last Post: 25 September 2012 - 08:32 PM Rate Topic: -----

#1 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

vtables and templates

Posted 25 September 2012 - 07:15 PM

Greetings DIC. Below is my code I've been working on


AI State Interface
template <class entity_type>
class IAIState
{
public:
	virtual ~IAIState(){}
	virtual void enter( entity_type* ) = 0;
	virtual void execute( entity_type* ) = 0;
	virtual void exit( entity_type* ) = 0;
};



AI State implementing Interface
class AIStateWander : public IAIState<Animal>
{
public:
	AIStateWander(){}
	virtual ~AIStateWander();

	AIStateWander( const AIStateWander& );
	AIStateWander& operator=( const AIStateWander& );

	void enter( Animal* animal );
	void execute( Animal* animal );
	void exit( Animal* animal );
};


Animal header for reference
class Animal : public BaseObject
{
public:
	Animal( SDL_Surface* img, float posX, float posY );
	virtual ~Animal();

	virtual void update( int width, int height );
	virtual void draw( SDL_Surface* src, const Camera& cam );
	virtual void clean();

	AIStateMachine<Animal>* getFSM() { return m_pAIStateMachine; }

private:
	AIStateMachine<Animal>* m_pAIStateMachine;
};



Now I'm getting an error on the declaration of my constructor of the AIWanderState
Error = "undefined reference to vtable for AIWanderState"

I know the vtable is created from my BaseObject class, and my Animal class overrides it. I've also got another vtable from my IAIState class, and my AIWanderState overrides the virtual functions there. Now my question is, does the templated base class that AIStateWander is inheriting from cause confusion with the vtables?

Is This A Good Question/Topic? 0
  • +

Replies To: vtables and templates

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1327
  • View blog
  • Posts: 4,552
  • Joined: 19-February 09

Re: vtables and templates

Posted 25 September 2012 - 08:06 PM

Would templating the derived class make more sense?

00  template <class entity_type>
01  class AIStateWander : public IAIState<entity_type>
02  {


Was This Post Helpful? 0
  • +
  • -

#3 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

Re: vtables and templates

Posted 25 September 2012 - 08:32 PM

I think I'm starting to see the problem here. Here's a snippet of my StateManager class


template <class entity_type>
class AIStateMachine
{
public:

	void setCurrentState( IAIState<entity_type>* s ) { m_pCurrentState = s; }
	void setGlobalState( IAIState<entity_type>* s ) { m_pGlobalState = s; }
	void setPreviousState( IAIState<entity_type>* s ) { m_pPreviousState = s; }

//More Functions
}



I'm templating my StateMachine class, and from there templating my State Interface. In the AIStateWander, I have a "const" base template class, I don't know if that's valid or not.

I want my AIStateWander class to be available only for the Animal class, and to do that, I'd have to have a templated IAIState<Animal> class. However I don't know if that is legal or not.



EDIT - I got my code to compile, turns out I had never defined my virtual destructor. Such a simple problem :D

This post has been edited by nunc: 25 September 2012 - 09:00 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1