Getting an error with the callback function on Keyboard Pressed event

Did you make my changes?

Your provided files also do not compile with my compiler without my changes.

  1. What @IQD said.

  2. This should work in Visual Studio just fine.

You could try manually expanding the callback Macro to the following as it might give you better error report

auto listener = EventListenerKeyboard::create();
listener->onKeyPressed = std::bind(&KeyPressed::Pressed, this, std::placeholders::_1, std::placeholders::2);

Also I do sometimes prefer the non-lambda method as a way to keep the code structured, but if the lambda version works for now you should just use that so you can get real work done. Just have a setup method that only creates the event listener and its associated lambdas to separate it from other init code.

After making the change…

Sure, but Pressed is in the KeyPressed class and he passed this(which is a different class) as a second parameter, which would not compile anyway.

I love lambdas, because the are in place and concise.
Non lambda methods may provide better structured code, but add a lot of letterhead code.

Another issue is that code for callbacks is to relying on virtual functions.

You forgot to add this one:

KeyPressed *keyPressed = new KeyPressed();

Keep in mind this is just a quick hack to narrow down your issue.

Thx iQD its working now :grinning: btw what was the issue…

You have two errors in your code:

  1. You return a bool in onTouchMoved, but the function needs void.
    You can comment out the CC_CALLBACK_2 code and return a bool in the onTouchMoved lambda again and you will see, that the main issue was not the one you described in the topic. It was just a followup error.

  2. You are binding to a member function without an object.

    _listener->onKeyPressed = CC_CALLBACK_2(KeyPressed::Pressed, this);

The Pressed function is not implemented in the class, which is calling it, so you need an object of the class implementing it, which is KeyPressed. Don’t forget to delete the object again or use smart pointers or the auto-release functionality of cocos2d-x.

You should not create a listener on an object in a different class anyway. Always create the listener in the class, which implements the callback unless it’s necessary for your logic.

@slackmoehrle Btw. both .h files are private.

The page you requested doesn’t exist or is private.

Hi Everyone,
Even I am getting the same error.
What should I do to remove that.
I am not sure from which line is this error coming.