method pointer question

i'm trying to use method pointers to simplify my logic. however, i'm getting compile errors and i dont know why. any help would be appreciated.

-- in my header

private:
typedef void (MyClass::*StateFuncPtr)();
StateFuncPtr stateFuncPtr;

void StateNoStringFunc();
void StateInStringFunc();

...

-- in my .cpp

MyClass::MyClass()
{
stateFuncPtr = &MyClass::StateNoStringFunc;
stateFuncPtr();

...

i get the error

error C2064: term does not evaluate to a function taking 0 arguments

and i have no idea why since the method pointer has no args as do the method it's assigned to. thanks.
I think you have some problem in row
typedef void (MyClass::*StateFuncPtr)();
If you will post whole program I will try it.
Typedef is using to define own type name for existing typ.
For example:
typedef long int BigOnes;
BigOnes mynum = 0L;
thanks. here's a stripped down version of the class

//lexer.h
class Lexer
{
public:
Lexer();

private:
typedef void (Lexer::*StateFuncPtr)();
StateFuncPtr stateFuncPtr;

void StateNoStringFunc();
void StateInStringFunc();
void StateEndStringFunc();
};
//lexer.cpp

#include "stdafx.h"
#include "Lexer.h"

Lexer::Lexer()
{
stateFuncPtr = &Lexer::StateNoStringFunc;
stateFuncPtr();
}


void Lexer::StateNoStringFunc()
{
}

void Lexer::StateInStringFunc()
{
}
you must use (this->*stateFuncPtr)(); instead of stateFuncPtr();
I don't know why you cann't call only by stateFuncPtr(). I think that this is syntax problem.
The answer is because every member function, by virtue of being a member function, has an implicit "this" pointer as its first parameter.
Attempting to call the member function via pointer, you must specify
which object you are calling the function on. Hence why (this->*f)().
Calling it like f() you are not telling the compiler which object you are
calling the member function on.

Topic archived. No new replies allowed.