I’ve created a singleton pattern but I get an external link error. I’ve searched everywhere on the net but no fix seems to work.
Error 1 error LNK2001: unresolved external symbol "private: static class MySingleton * MySingleton::m_mySingleton" (?m_mySingleton@MySingleton@@0PAV1@A) C:\Users\User\Desktop\cocos2d\MyProgram\MySingleton.obj
Error 2 error LNK1120: 1 unresolved externals C:\Users\User\Desktop\cocos2d\Debug.win32\MyProgram.win32.exe 1
This is how I implemented it:
//MySingleton.h
#ifndef _MYSINGLETON_H_
#define _MYSINGLETON_H_
#include "cocos2d.h"
class MySingleton
{
private:
//Constructor
MySingleton();
//Instance of the singleton
static MySingleton* m_mySingleton;
public:
//Get instance of singleton
static MySingleton* mySingleton();
//A function that returns zero "0"
int ReturnZero(){return 0;}
};
#endif //_MYSINGLETON_H_
//MySingleton.cpp
#include "MySingleton.h"
MySingleton::MySingleton()
{
}
MySingleton* MySingleton::mySingleton(void)
{
//If the singleton has no instance yet, create one
if(NULL == m_mySingleton)
{
//Create an instance to the singleton
m_mySingleton = new MySingleton();
}
//Return the singleton object
return m_mySingleton;
}
Then I call the simple methode in the singleton like this:
//in HelloWorldScene.cpp
int zero = MySingleton::mySingleton()->ReturnZero();