Function Pointer not behaving

I am trying to build a class (Player) that contains a few functions, and a function pointer that points to one of those functions. I wish to call that function from another function by way of the function pointer.

I declare the function pointer like so in Player.h:
void (Player::*strat)(int);

I assign it like so in a another function in Player.cpp:
strat = &Player::tittat;

I then attempt to call the function like so from another function in Player.cpp:
strat(i);

The above call is supposed to execute, in this example, the function tittat(i). Instead, I get this stupid error:
"error C2064: term does not evaluate to a function taking 1 arguments"

I have tried everything I can think of to resolve this issue. How do I fix this?
I'll assume yo have an object of type Player named player:
(player.*strat)(i);
The first set of parentheses is NOT optional.
Last edited on
It sounds like each of these functions belongs in a class of its own, all subclassed from the same abstract base class. The Player would then have an object of this type as a member. It's easier to work with functions as objects this way.

What you are trying to do sounds very much like what the strategy pattern solves: http://en.wikipedia.org/wiki/Strategy_pattern

To answer your question directly, you have to assign strat an object member function, not a class member function.

1
2
Player  p;
strat = &p.tittat;
Thanks, that strategy patter will definitely come in handy! Gosh, this just goes to show what a C++ n00b I am: Where would one initialize the object p? Would I need to do that in the class where I implement the Player class, or could I do it in the Player class itself? Keep in mind, I wish to be able to do this:

1
2
3
4
5
void Player::play(int i)
{
	/*some stuff*/
	strat(i);
}


Also, each function is a member of the player class and not of its own.
Topic archived. No new replies allowed.