Singleton Object not working on cocos2d-x

Singleton Object not working on cocos2d-x
0.0 0



I am trying to create Singleton Object but I am getting an error. I don’t know whats the problem…

.h file

#ifndef __MY_CC_SPRITE__
#define __MY_CC_SPRITE__

class MyCCSprite

    // Private

    // Constructor

    // Initializer
    void Init();

    // Variables
    static MyCCSprite* MyCCSpriteObject;

    // Public

    // Virtual Functions
    static MyCCSprite* getInstance();

#endif // __MY_CC_SPRITE__

.cpp file

#include "MyCCSprite.h"
#include "cocos2d.h"

using namespace std;

// Constructor
    printf("--- CONSTRUCTOR [MyCCSprite] ---\n");

MyCCSprite* MyCCSprite::getInstance() {
    printf("--- GETINSTANCE [MyCCSprite] ---\n");
    if(MyCCSpriteObject == NULL) MyCCSpriteObject = new MyCCSprite();
    return MyCCSpriteObject;

Following ERROR Received when declared its instance in HelloWorld.cpp

// MyCCSprite
MyCCSprite *mySprite1 = MyCCSprite::getInstance();

Ld build/Debug-iphonesimulator/ normal i386
cd /Users/pilabsmacmini2/Projects/COCOS2DX/TapCards
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/g++-4.2 -arch i386 -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk -L/Users/pilabsmacmini2/Projects/COCOS2DX/TapCards/build/Debug-iphonesimulator -F/Users/pilabsmacmini2/Projects/COCOS2DX/TapCards/build/Debug-iphonesimulator -filelist /Users/pilabsmacmini2/Projects/COCOS2DX/TapCards/build/ -mmacosx-version-min=10.6 -ObjC -all_load -Xlinker -objc_abi_version -Xlinker 2 -framework CoreGraphics -framework Foundation -framework OpenGLES -framework QuartzCore -framework UIKit -framework AudioToolbox -framework OpenAL -lz -framework AVFoundation "-lcocos2d libraries" -lxml2 -o /Users/pilabsmacmini2/Projects/COCOS2DX/TapCards/build/Debug-iphonesimulator/

Undefined symbols:
  "MyCCSprite::MyCCSpriteObject", referenced from:
      __ZN10MyCCSprite16MyCCSpriteObjectE$non_lazy_ptr in MyCCSprite.o
     (maybe you meant: __ZN10MyCCSprite16MyCCSpriteObjectE$non_lazy_ptr)
ld: symbol(s) not found
collect2: ld returned 1 exit status


I don’t think that this problem is related to cocos2d.

Try inserting this into your cpp above the constructor:
MyCCSprite* MyCCSprite::MyCCSpriteObject = 0;


Not working :frowning:

Undefined symbols:

Regardless of the code I posted above, you never define the static member variable “MyCCSpriteObject” located in the MyCCSprite class.