1

I'm writing an event system for input where i store vectors of callbacks for any used key. All these templates are going to be member functions with one float as an argument, so i use std::bind and a placeholder for this. I have a function within a Key class that adds the callback to the respective vector and i want to do the binding within that function, but i'm running into a problem and i can't find any information on how to solve it.

The Key header file has the following prototype for adding the callbacks:

template <class T>
void addOnPressed(void toCall(float), T *callOn);

And this is what the function actually looks like:

template <class T>
void Key::addOnPressed(void toCall(float), T *callOn) {
    onPressed.push_back(std::move(std::bind(toCall, callOn, std::placeholders::_1)));
}

To test it all i made a Player class that adds some callbacks on construction, this constructor looks like this:

-Player(Texture2D Texture, int LayerIndex, Input &Input) : InputObj{ Texture, LayerIndex, Input } {
    input.keyboard.getKey(KeyboardKey::A).addOnPressed<Player>(&Player::moveLeft, this);
    input.keyboard.getKey(KeyboardKey::D).addOnPressed<Player>(&Player::moveRight, this);
    input.keyboard.getKey(KeyboardKey::W).addOnPressed<Player>(&Player::moveUp, this);
    input.keyboard.getKey(KeyboardKey::S).addOnPressed<Player>(&Player::moveDown, this);
};

All of this gives me the following error:

C2664 'void Key::addOnPressed<Player>(void (__cdecl *)(float),T *)': cannot convert argument 1 from 'void (__cdecl Player::* )(float)' to 'void (__cdecl *)(float)

I'm guessing i need to somehow tell the addOnPressed function that the given function pointer is from the class T and i tried using the syntax given in the error message, but all i got was syntax errors.

1 Answers1

0

The error message is quite clear, addOnPressed takes a non-member function pointer as its 1st parameter, while you're passing member function pointers.

You might change the parameter type to member function pointer, e.g.

template <class T>
void addOnPressed(void (T::* toCall)(float), T *callOn)
songyuanyao
  • 169,198
  • 16
  • 310
  • 405