单例模式 c++错误
您好,我已经使用 VS2010 在 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" (?GpuManager@EgEngine@@0VEgGpuManager@@A)
请帮助我,谢谢。
Hi I have implemented a singleton pattern in c++ with the VS2010 and the compiler throw me an error !!
#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
The other class
#ifndef __EgGpuManager__
#define __EgGpuManager__ 1
#include <EgBuffer.h>
#include <EgProgram.h>
class EgGpuManager
{
public:
EgBuffer* GetBuffer();
EgProgram* GetNewProgram();
private:
EgGpuManager();
~EgGpuManager();
EgBuffer buffer;
};
#endif
And when I try to compile I have this error:
1>EgEngine.obj : error LNK2001:
unresolved external symbol "private: static class
EgGpuManager EgEngine::GpuManager" (?GpuManager@EgEngine@@0VEgGpuManager@@A)
Help me please and thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
静态 EgGpuManager GpuManager; // Return this !!
这个家伙必须在你的 C++ 代码中的某个地方实例化。static
类成员必须出现在全局范围内,因此在 C++ 文件中添加:EgGpuManager EgEngine::GpuManager
。顺便说一句,您有一个
EgGpuManager
类的私有构造函数,在这种情况下这将是一个问题,因为它是由EgEngine
创建的。您没有正确实现单例。使用static EgGpuManager *EgGpuManager::Get()
方法返回一个实例,它将在第一次调用时实例化该类,然后您可以使用私有构造函数来完成它。否则就让他们成为朋友
。static EgGpuManager GpuManager; // Return this !!
this guy has to be instantiated somewhere in your C++ code.static
class members have to appear in the global scope, so in the C++ file add:EgGpuManager EgEngine::GpuManager
.By the way, you have a private constructor for
EgGpuManager
class, which will be a problem in this case because it is created byEgEngine
. You're not implementing the singleton correctly. Use astatic EgGpuManager *EgGpuManager::Get()
method to return an instance, and it will instantiate the class on the first call, then you can do it with a private constructor. Otherwise make themfriend
s.“static EgGpuManager GpuManager;”行EgEngine 的类声明内部只有一个声明:你说这个对象将存在于某个地方。链接器抱怨它没有在任何地方找到该对象。要解决此问题,请在源文件之一中放置一个实例化(在全局范围内):
The line "static EgGpuManager GpuManager;" inside the class declaration of EgEngine is only a declaration: You're saying that this object will exist somewhere. The linker complains that it did not find the object anywhere. To solve this, place an instantiation in one of your source files (in global scope):
您必须将单例对象的实例化放在全局范围内(如其他答案所建议),或者放在
GetGpuManager()
的实现中,如下所示:在这种情况下,您需要删除 GpuManager 的声明从类定义。另请注意 &返回引用,因为您当然不想返回对象的副本(这会破坏创建单例的目的)。这样做的优点是,直到第一次调用 GetGpuManager() 才会创建对象,而全局范围内的所有静态变量都是在程序启动时创建的。
You must put the instantiation of the singleton object either in the global scope, as suggested by other answers, or in the implementation of
GetGpuManager()
like this :In this case you need to remove the declaration of GpuManager from the class definition. Also note the & to return a reference, as you certainly don't want to return a copy of the object (that would defeat the purpose of making a singleton). The advantage here is that the object will not be created until
GetGpuManager()
is called for the first time, whereas all statics in global scope are created when the program starts.