C++ 单例错误

C++ 单例错误,c++,undefined-reference,C++,Undefined Reference,我为我的游戏实现了一个单例类,但每次我试图编译它时,它都会给出一个编译错误。代码如下: //Singleton.h #pragma once class Singleton{ public: static Singleton* Instance(); private: Singleton(){}; static Singleton* m_instance; }; Singleton*

我为我的游戏实现了一个单例类,但每次我试图编译它时,它都会给出一个编译错误。代码如下:

    //Singleton.h
    #pragma once

    class Singleton{
    public:
        static Singleton* Instance();

    private:
        Singleton(){};

        static Singleton* m_instance;
    };

    Singleton* SingletonInstance = Singleton::Instance();
Singleton.cpp

//Singleton.cpp
#include "Untitled1.h"

Singleton* m_instance = nullptr;


Singleton* Singleton::Instance(){
    if(m_instance == nullptr){
        m_instance = new Singleton;
    }
    return m_instance;
}
我收到以下错误:

||=== Build: Debug in df (compiler: GNU GCC Compiler) ===| obj\Debug\Untitled1.o||In function `ZN9Singleton8InstanceEv':| C:\Users\Samsung R519\Desktop\Untitled1.cpp|7|multiple definition of `SingletonInstance'| obj\Debug\main.o:C:\Users\Samsung R519\Desktop\main.cpp|4|first defined here| obj\Debug\Untitled1.o:Untitled1.cpp|| undefined reference to `Singleton::m_instance'| obj\Debug\Untitled1.o:Untitled1.cpp|| undefined reference to `Singleton::m_instance'| ||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 10 second(s)) ===| ||==构建:在df中调试(编译器:GNU GCC编译器)===| 函数“ZN9Singleton8InstanceEv”中的obj\Debug\Untitled1.o | |:| C:\Users\Samsung R519\Desktop\Untitled1.cpp | 7 | `singletonistance'的多重定义| obj\Debug\main.o:C:\Users\Samsung R519\Desktop\main.cpp | 4 |首先在这里定义| obj\Debug\Untitled1.o:Untitled1.cpp | |对'Singleton::m|u instance'的未定义引用| obj\Debug\Untitled1.o:Untitled1.cpp | |对'Singleton::m|u instance'的未定义引用| ||==生成失败:4个错误,0个警告(0分钟,10秒))===|
我做错了什么?

在.cpp文件中,您正在定义一个新的
m_实例,它不是成员

请试一试

Singleton* Singleton::m_instance = nullptr;

尽管它们具有相同的名称,但它们是不同的指针,因为它们具有不同的作用域:第一个是类成员的声明,第二个是全局命名空间中指针的声明和定义

因此,您要查找的是类成员指针的定义,即:

Singleton* Singleton::m_instance = nullptr;

您的代码有两个问题

  • Singleton*singletonistance=Singleton::Instance()不应将定义放入头文件中。相反,您应该在头文件中声明
    singletonistance
    ,并在.cpp文件中定义它。事实上,不需要这个全局变量。您可以随时调用
    Singleton::Instance()
    来获取Singleton

  • m_instance
    Singleton
    的成员,因此您应该使用类名定义它:
    Singleton::m_instance

  • 解决方案:

    // xxx.h
    // other code...
    extern Singleton* SingletonInstance; // declaration
    
    // xxx.cpp
    // other code...
    Singleton* Singleton::m_instance = nullptr;   // specify the class name
    
    Singleton* SingletonInstance = Singleton::Instance(); // definition
    

    我建议你用Scott Meyers单件。我将避免任何与初始化顺序相关的错误,以及任何与单例相关的内存管理错误

    下面是您如何实现它的:

    struct Singleton {
        static Singleton& instance() {
            static Singleton myInstance;
    
            return myInstance;
        }
    };
    

    如果你想返回一个指针而不是一个引用,这同样很好。

    重新考虑一下(a)为什么你需要一个单例:它几乎总是一个反模式;(b)为什么在这里使用指针。除非需要延迟初始化,否则使用具有自动存储并通过引用返回的简单对象几乎肯定是优越的。在C++中实现singleton的另一种(更好的?)方法的可能重复:它起了作用,但现在出现了这个错误:“singleton.cpp | 7 | singleton Stance”的多个定义
    // xxx.h
    // other code...
    extern Singleton* SingletonInstance; // declaration
    
    // xxx.cpp
    // other code...
    Singleton* Singleton::m_instance = nullptr;   // specify the class name
    
    Singleton* SingletonInstance = Singleton::Instance(); // definition
    
    struct Singleton {
        static Singleton& instance() {
            static Singleton myInstance;
    
            return myInstance;
        }
    };