您好我已经在VS2010中使用C++实现了一个单例模式,并且编译器给我一个错误!Singleton模式C++错误
#ifndef __EgEngine__
#define __EgEngine__ 1
#include <esUtil.h>
#include <stdlib.h>
#include <EgGpuManager.h>
class EgEngine
{
public:
EgEngine();
static EgGpuManager GetGpuManager();
~EgEngine();
void EgInit();
private:
EgEngine(const EgEngine &other){};
EgEngine* operator = (const EgEngine &other)const {};
static EgGpuManager GpuManager; // Return this !!
ESContext esContext;
};
#endif
其他类
#ifndef __EgGpuManager__
#define __EgGpuManager__ 1
#include <EgBuffer.h>
#include <EgProgram.h>
class EgGpuManager
{
public:
EgBuffer* GetBuffer();
EgProgram* GetNewProgram();
private:
EgGpuManager();
~EgGpuManager();
EgBuffer buffer;
};
#endif
当我尝试编译我有这样的错误:
1>EgEngine.obj : error LNK2001:
unresolved external symbol "private: static class
EgGpuManager EgEngine::GpuManager" ([email protected]@@[email protected]@A)
请帮助我和感谢。
可能重复[静态成员变量在类中](http://stackoverflow.com/questions/5601051/static-member-variable-in-a-class) –
[不要使用单身人士](http: //jalf.dk/blog/2010/03/singletons-solving-problems-you-didnt-know-you-never-had-since-1995/)。他们并不酷。在多线程环境中,它们很难正确实现,几乎不可能实现,即使它们正确实现,它们仍然只会使代码变得更糟。 – jalf
此外,应避免像__EgEngine__这样的名称。包含双下划线的名称,或者以下划线开头,后跟大写字母的名称被保留供实现(编译器和标准库)使用(最后,制作宏ALL UPPERCASE是常见做法),所以它应该是' EGENGINE',而不是'EgEngine'。 – jalf