I am writing a program where I am using one class for functionalatiy and one class for storage ("Game" and "Player" respectively). Depending on what parameters are passed to Game, different numbers of players will be created, each using a different strategy, the strategies being functions defined in Game. As such, I have a function pointer in Player that I wish to point to a strategy function in Game (declared as "void (*strat)(int);").
I am trying to use a function in Game to facilitate this transaction:
You cannot convert a pointer to a global function (strat) to a non-global member function (sp) or vice versa. Nonstatic member functions take an additional hidden parameter (this) and therefore are incompatible with global functions.
You'll need to change the way strat is defined.
You also might want to consider using typedefs to make your life easier:
Bazzy,
I need strat to be reinitialized for every instance of Player I create. I therefore cannot move it into the Game class. Is there a way I can declare it as a member of the game class without excluding it from the Player class?
class Game
{
void AStrategyFunction(int foo)
{
// do stuff
}
};
typedefvoid (Game::*StratFunc)(int); // typedef it for ease
class Player
{
// make the pointer a member of Player
StratFunc strat;
Player()
{
// the ctor
strat = &Game::AStrategyFunction; // although strat is a member of
// Player, it points to a member of Game
}
};