Game Development Community

GCC, function pointers and virtual functions...

by Stefan Beffy Moises · in Torque Game Engine · 06/12/2003 (8:29 am) · 6 replies

Let me post a nice error message here... ;)
--> Compiling game/ai/State.cc
--> Compiling game/ai/FSM.cc 
game/ai/FSM.cc: In constructor 'CFSM::CFSM()': 
game/ai/FSM.cc:8: error: no matching function for call to 'CStateTemplate<CFSM> 
::Set(CFSM* const, <unknown type>, <unknown type>, <unknown type>)' 
game/ai/State.h:43: error: candidates are: void CStateTemplate<T>::Set(T*, void 
(T::*)(), void (T::*)(), void (T::*)()) [with T = CFSM] 
make[1]: *** [out.GCC2.DEBUG/game/ai/FSM.obj] Fehler 1 
gmake: *** [default] Fehler 2 
*** failed ***
This is what GCC gives me on SuSE Linux 8.2....
that exact same code compiles fine without any warnings or errors on Windows/VC6 or 7...
the code is from the GPG3 book article "Function Pointer-Based, Embedded Finite-State-Machines" by Charles Farris...
Here is the beginning of that class so that you see what is going on:
template <class T>
class CStateTemplate : public CState
{
protected:
	typedef void (T::*PFNSTATE)(void);
	T *m_pInstance;									// Instance Pointer
	PFNSTATE m_pfnBeginState;						// State Function Pointer
	PFNSTATE m_pfnState;							// State Function Pointer
	PFNSTATE m_pfnEndState;							// State Function Pointer

public:
	// Constructor
	CStateTemplate() : m_pInstance(0),m_pfnBeginState(0),m_pfnState(0),m_pfnEndState(0) {}

	// Initialize Functions
	void Set(T *pInstance,PFNSTATE pfnBeginState,PFNSTATE pfnState,PFNSTATE pfnEndState)
	{

And here is the FSM class constructor using this State Template:
CFSM::CFSM()
{
	// Initialize States
	m_StateInitial.Set(this,BeginStateInitial,StateInitial,EndStateInitial);
	// Initialize State Machine
	m_pCurrentState=static_cast<CState*>(&m_StateInitial);
	m_pNewState=0;
}
BeginStateInitial etc. are virtual functions declared in FSM.cc:
virtual void BeginStateInitial() {}
	virtual void StateInitial() {}
	virtual void EndStateInitial() {}


Does anybody have any idea why GCC doesnt like this? I was looking for other examples on Google, but they all looked kinda similar...

thx for any idea!

#1
06/12/2003 (8:47 am)
Are you using gcc3? If not, might try that, it has better c++ support than gcc2.

One difference I noticed is that gcc thinks "this" is a CFSM* const, whereas the function is declared as taking a regular T pointer (no const). Maybe a const_cast in the constructor would work:

m_StateInitial.Set(const_cast<CFSM*>(this),BeginStateInitial,StateInitial,EndStateInitial);

If that doesn't do it, you could try a reinterpret_cast

m_StateInitial.Set(reinterpret_cast<CFSM*>(this),BeginStateInitial,StateInitial,EndStateInitial);
#2
06/12/2003 (9:42 am)
John,
thx a lot for your suggestions!
I'll try them l8r (guest coming in a minute ;)
Yep, thats on GCC 2/KDevelop, maybe I should really try 3...
I'll let you know if that solves it!
#3
06/12/2003 (1:09 pm)
Hm, the casts dont help, seems like the problem is the 3 virtual functions passed in there...?
linux:/home/beffy/cvslocal/torque # make
--> Compiling game/ai/FSM.cc
game/ai/FSM.cc: In constructor 'CFSM::CFSM()':
game/ai/FSM.cc:8: error: no matching function for call to 'CStateTemplate<CFSM>
   ::Set(CFSM*, <unknown type>, <unknown type>, <unknown type>)'
game/ai/State.h:45: error: candidates are: void CStateTemplate<T>::Set(T*, void
   (T::*)(), void (T::*)(), void (T::*)()) [with T = CFSM]
make[1]: *** [out.GCC2.DEBUG/game/ai/FSM.obj] Fehler 1
make: *** [default] Fehler 2
And here the reinterpret_cast:
linux:/home/beffy/cvslocal/torque # make
--> Compiling game/ai/FSM.cc
game/ai/FSM.cc: In constructor 'CFSM::CFSM()':
game/ai/FSM.cc:8: error: no matching function for call to 'CStateTemplate<CFSM>
   ::Set(CFSM*, <unknown type>, <unknown type>, <unknown type>)'
game/ai/State.h:45: error: candidates are: void CStateTemplate<T>::Set(T*, void
   (T::*)(), void (T::*)(), void (T::*)()) [with T = CFSM]
make[1]: *** [out.GCC2.DEBUG/game/ai/FSM.obj] Fehler 1
make: *** [default] Fehler 2
Guess my last option is to try GCC3.... weird...
#4
06/12/2003 (1:23 pm)
Doh, it IS GCC 3 already... :/
# /usr/bin/gcc --version
gcc (GCC) 3.3 20030226 (prerelease) (SuSE Linux)
now I'm lost :P
Or do you have some more ideas?
#5
06/12/2003 (4:13 pm)
It does seems like it is the function pointer types. Where is PFNSTATE defined? Is that header file being included?
#6
06/12/2003 (9:24 pm)
John-
I've finally solved it,
what worked was this:
m_StateInitial.Set(this,&CFSM::BeginStateInitial,&CFSM::StateInitial,&CFSM::EndStateInitial);
Don't ask me why, but that did the trick for GCC... :P
Thx again for your help!
beffy