xcode 成员函数指针错误:必须调用对非静态成员函数的引用

txu3uszq  于 2022-12-19  发布在  其他
关注(0)|答案(3)|浏览(131)
this->scheduleOnce(schedule_selector(SelectGameScene::startGameCallback),this, 0.0f, false);

出现错误:必须调用对非静态成员函数的引用。

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

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

地点

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

我在使用C++11标准和cococ 2d-x ver4.0库的XCode上遇到了这个错误。

**更新:**我尝试了此代码

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

出现错误使用未声明的标识符'schedule_selector'

Update 2我发现了问题。我通过静态方法createScene创建了这个类。

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

nqwrtyyt1#

sytax SelectGameScene::startGameCallback无效。它必须具有&

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

hgtggwj02#

XCode编译器认为SelectGameScene::startGameCallback是静态方法,但它只是一个成员函数指针,所以我决定重写这条语句。

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

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

因为

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

x33g5p2x3#

此错误可在v4中重现。它是由“CCRef. h”中的已弃用宏引起的,这些宏现已消失。
只需将“schedule_selector”替换为“CC_SCHEDULE_SELECTOR”即可。

相关问题