Search code examples
c++xcodec++11cocos2d-x

Member function pointer error: Reference to non-static member function must be called


this->scheduleOnce(schedule_selector(SelectGameScene::startGameCallback),this, 0.0f, false);

I got an error : Reference to non-static member function must be called.

void startGameCallback(float dt); //in h file

void SelectGameScene::startGameCallback(float dt)
{
    Director::getInstance()->replaceScene(TransitionFade::create(TRANSITION_TIME,     GameScene::createScene()));
}

Where

#define CC_SCHEDULE_SELECTOR(_SELECTOR) static_cast<cocos2d::SEL_SCHEDULE>(&_SELECTOR)
typedef void (Ref::*SEL_SCHEDULE)(float);

I got this error on XCode with c++11 standard and cococ2d-x ver4.0 library.

Update: I tried this code

this->scheduleOnce(schedule_selector(&SelectGameScene::startGameCallback),this, 0.0f, false);

I got an error Use of undeclared identifier 'schedule_selector'

Update2 I found the problem. I created this class through static method createScene.

class SelectGameScene : public cocos2d::Layer
{
 public:
   static cocos2d::Scene* createScene();
 }

Solution

  • XCode compiler thinks that SelectGameScene::startGameCallback is static method but it is just a member function pointer. So I decide to rewrite this statement.

    from

    this->scheduleOnce(schedule_selector(SelectGameScene::startGameCallback),0.0f);
    

    to

    auto funPointer = static_cast<cocos2d::SEL_SCHEDULE>(&SelectGameScene::startGameCallback);
    this->scheduleOnce(funPointer, 0.0f);
    

    because

    #define CC_SCHEDULE_SELECTOR(_SELECTOR) static_cast<cocos2d::SEL_SCHEDULE>(&_SELECTOR)