我收到以下错误,但是我的代码应该没有问题。由于我已经声明了createScene方法。如果有一些技巧或提示可能会导致解决此问题,那就太好了!

AppDelegate.cpp:82: error: undefined reference to 'MainScene::createScene()'
collect2: error: ld returned 1 exit status


AppDelegate.cpp:82:这是发生错误的地方

// create a scene. it's an autorelease object
auto scene = MainScene::createScene();


这是MainScene.cpp

#include "MainScene.h"

USING_NS_CC; //Means using cocos2d

MainScene::MainScene() {

}

MainScene::~MainScene() {

}

Scene* MainScene::createScene() {

   auto scene = Scene::create();
   auto layer = MainScene::create();

   scene->addChild(layer);
   return scene;
}

bool init() {
    if (!Layer::init()) {
        return false;
    }
    auto director = Director::getInstance();
    auto size = director->getWinSize();
    auto background = Sprite::create("background.png");
    background->setPosition(Vec2(size.width/2.0, size.height / 2.0));
    this->addChild(background);

    //initialize process
    return true;
}


这是MainScene.h

class MainScene: public cocos2d::Layer
{
protected:
    MainScene();
    virtual ~MainScene();
    bool init() override;

public:
    static cocos2d::Scene* createScene();
    CREATE_FUNC(MainScene);
};

最佳答案

我认为您忘记定义init()类的MainScene方法

bool init() {
    ...
    ...
}


更改为

bool MainScene::init(){
    ...
    ...
}

关于c++ - 未定义对“MainScene::createScene()”的引用collect2:错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43722500/

10-10 12:42