cocos2d-x Android中的单例

cocos2d-x Android中的单例,android,c++,android-ndk,singleton,cocos2d-x,Android,C++,Android Ndk,Singleton,Cocos2d X,我正试图编写一个用于维护游戏数据的单例类,它叫做GameManager,就像《Learning CoCoCos2D》一书一样 这是我的.h文件: 这是我的.cpp文件: 以下是HelloWorld.cpp中的调用: 这是一个奇怪的问题,它与xcode配合得很好,可以构建在iPhone上。但当我尝试使用ndk构建时: 如果未定义对“GameManager::sharedGameManager()”的引用,那么为什么第一个调用会起作用 任何帮助都可以,谢谢 你确定你已经将带有GameManager实

我正试图编写一个用于维护游戏数据的单例类,它叫做GameManager,就像《Learning CoCoCos2D》一书一样

这是我的.h文件: 这是我的.cpp文件: 以下是HelloWorld.cpp中的调用: 这是一个奇怪的问题,它与xcode配合得很好,可以构建在iPhone上。但当我尝试使用ndk构建时: 如果未定义对“GameManager::sharedGameManager()”的引用,那么为什么第一个调用会起作用


任何帮助都可以,谢谢

你确定你已经将带有GameManager实现的cpp文件(你称之为“这是我的.cpp文件”)包含到你的Android.mk文件中了吗?

谢谢你,Alex,我想这是因为singleton,但不是。我将cpp文件添加到我的Android.mk文件中,它可以正常工作。
#ifndef GameManager_h
#define GameManager_h

#include "cocos2d.h"

class GameManager
{
private:
    //Constructor
    GameManager();

    //Instance of the singleton
    static GameManager* m_mySingleton;

public:    
    //Get instance of singleton
    static GameManager* sharedGameManager();    

    //A function that returns zero "0" 
    int ReturnZero(){return 0;}
    // another test function
    void runScene() { CCLOG("test");};

};
#include "SimpleAudioEngine.h"
#include "GameManager.h" 
using namespace cocos2d;
using namespace CocosDenshion;

//All static variables need to be defined in the .cpp file
//I've added this following line to fix the problem
GameManager* GameManager::m_mySingleton = NULL;

GameManager::GameManager()
{    

}

GameManager* GameManager::sharedGameManager()
{
    //If the singleton has no instance yet, create one
    if(NULL == m_mySingleton)
    {
        //Create an instance to the singleton
        m_mySingleton = new GameManager();
    }

    //Return the singleton object
    return m_mySingleton;
}
void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event) {
    CCLOG("return zero:%d",GameManager::sharedGameManager()->ReturnZero());  // Line 231
    GameManager::sharedGameManager()->runScene();  // Line 232
}
./obj/local/armeabi/objs-debug/game_logic/HelloWorldScene.o: In function `HelloWorld::ccTouchesEnded(cocos2d::CCSet*, cocos2d::CCEvent*)':
/Users/abc/Documents/def/def/android/jni/../../Classes/HelloWorldScene.cpp:232: undefined reference to `GameManager::sharedGameManager()'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi/libgame_logic.so] Error 1