member function pointer - - set up ?

This topic is 4599 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

okay ive been working on setting this up for a couple days now.. ive ran into a slight or maybe not so slight problem.. this snippet should be enought to get my question across
////////////////////////////////////
/// Commands.h ////
class CCommands
{
/*STUFF*/
}
// JUST TO MAKE THINGS CLEAR


//////////////////////////////////////////
/// Console.h /////
#include "Commands.h"
class myGame  // for external reference
//
class CConsole
{
// member function pointer
int (myGame::*funcPtr)(CCommands*);
//
void init()
{
//OTHER STUFF

funcPtr = &myGame::consoleParser;
return;
}
//
int CConsole::OnKeyDown(WPARAM wParam)
{
//calls the pointed function
// not sure if this is how to call it ..
int result = funcPtr;
return 0;
}
}


///////////////////////////////
/// main.cpp ///

#include "Console.h"
#include "Commands.h"
class myGame
{
CConsole *myConsole

int consoleParser( CCommands* pCommand )
{
/*stuff*/
}
}
winMain()
{
myGame* thisGame
//Stuff
}


I get these errors when i compile my code Console.cpp(88): error C2027: use of undefined type 'myGame' Console.cpp(88): error C2276: '&' : illegal operation on bound member function expression looks as if i have missunderstood how to do this How should i set this up any help would be much thanked..

Share on other sites
This sounds like a classic circular include problem. See this article on how to resolve that kind of problem.

Share on other sites
Well thanks for that I got it to compile ;P

how would i dereference the pointer to call the function

int result = *funcPtr(&command);
int result = (myGame.*funcPtr)(&command);
int result = (*funcPtr)(&command);

does not work

mainly get this error
Console.cpp(305): error C2064: term does not evaluate to a function taking 1 arguments

that does not make sense its declared like this
int (myGame::*funcPtr)(CCommands*);

Share on other sites
myGame is a pointer so you use ->* to bind the function pointer.

int result = (myGame->*funcPtr)(&command);