单例模式 c++错误

发布于 2024-11-27 09:29:20 字数 1122 浏览 0 评论 0原文

您好,我已经使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贱贱哒 2024-12-04 09:29:20

静态 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 by EgEngine. You're not implementing the singleton correctly. Use a static 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 them friends.

北斗星光 2024-12-04 09:29:20

“static EgGpuManager GpuManager;”行EgEngine 的类声明内部只有一个声明:你说这个对象将存在于某个地方。链接器抱怨它没有在任何地方找到该对象。要解决此问题,请在源文件之一中放置一个实例化(在全局范围内):

EgGpuManager EgEngine::GpuManager;

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):

EgGpuManager EgEngine::GpuManager;
伴梦长久 2024-12-04 09:29:20

您必须将单例对象的实例化放在全局范围内(如其他答案所建议),或者放在 GetGpuManager() 的实现中,如下所示:

EgGpuManager& EgEngine::GetGpuManager()
{
  static EgGpuManager GpuManager;
  return GpuManager;
}

在这种情况下,您需要删除 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 :

EgGpuManager& EgEngine::GetGpuManager()
{
  static EgGpuManager GpuManager;
  return GpuManager;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文