Note that there are some explanatory texts on larger screens.

plurals
  1. POSingleton in cocos2d-x Android
    primarykey
    data
    text
    <p>I'm trying to write a singleton class for maintain game data, It's called GameManager, just like the book "Learning cocos2d" produced.</p> <h2>here is my .h file:</h2> <pre><code>#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");}; }; </code></pre> <h2>and here is my .cpp file:</h2> <pre><code>#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; } </code></pre> <h2>Here is the call in HelloWorld.cpp:</h2> <pre><code>void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event) { CCLOG("return zero:%d",GameManager::sharedGameManager()-&gt;ReturnZero()); // Line 231 GameManager::sharedGameManager()-&gt;runScene(); // Line 232 } </code></pre> <h2>Here is the weird problem, It's worked fine with xcode, can build on iPhone. but when i try to build with ndk:</h2> <pre><code>./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 </code></pre> <p>If undefined reference to `GameManager::sharedGameManager()', why the hell the first call is working?</p> <p>Any help will do, thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload