How do you create a instance of a singleton that can be used in other classes?
For Example:
//Singleton_Class.h
#ifndef Singleton_Class
#define Singleton_Class
class Singleton
{
private:
static Singleton _instance;
Singleton() {}
~Singleton() {}
Singleton(const Singleton &);
Singleton & operator=(const Singleton &);
public:
static Singleton &getInstance(){return _instance;}
};
Singleton Singleton::_instance;
#endif
//Main.cpp
#include "Singleton_Class.h"
int main()
{
Singleton &s = Singleton::getInstance(); //Use the instance
}
//ClassA.cpp
#include "Singleton_Class.h"
class A
{
public:
A(){};
};
I get a linking error when trying to include the single class header for class A (LNK2005): "private: static class Singleton Singleton::_instance" (?_instance@Singleton@@0V1@A) already defined in Singleton Class.obj