I was implementing a singleton class for my game but every time I try to compile it, it gives a compilation error. The code is as follows :
//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;
}
I am getting the following errors:
||=== 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)) ===|
What am I doing wrong?