0

I have this following struct (in the header file):

struct StateTransitions {
    State currentState;
    State nextState;
    void (*fn)();

    StateTransitions (State currentState, State nextState, void (*fn)())
    {

    }
};

And I am trying to push some instances of this struct into a vector (inside .cpp file):

void MyClass::Start()
{

}

transitions.push_back(StateTransitions(Low, Started, &MyClass::Start));

I cannot get this to compile. I am not sure if my declaration of the constructor inside the class is correct.

What is the correct way to add a constructor so I can't add this type of objects to my vector ?

I am using pre-C++11.

Adrian
  • 19,440
  • 34
  • 112
  • 219

1 Answers1

2

You cannot pass a pointer to a member function as a static function. The reason is that a member function needs something to be passed to the this pointer, while a static function does not (and the compiler needs to check the type of whatever you are passing as this).

See also: Function pointer to member function

Community
  • 1
  • 1
szym
  • 5,606
  • 28
  • 34