Hi all,
I'm trying to re-learn c++ after being away for 4 or 5 years. I'm trying to create a singleton class, but when I attempt to link I get an error.
Here is the header (in file singleton.h):
1 2 3 4 5 6 7 8 9 10 11 12 13
#pragma once
class singleton
{
public:
typedef singleton * singletonPtr;
private:
static singletonPtr mySingleton;
singleton();
public:
singletonPtr getTheSingleton();
};
Here is the body (in file singleton.cpp):
1 2 3 4 5 6 7 8 9 10 11 12
#include "singleton.h"
singleton::singleton()
{
}
singleton::singletonPtr singleton::getTheSingleton()
{
if (!mySingleton)
mySingleton = new singleton();
return mySingleton;
}
And the linker message I get:
1>singleton.obj : error LNK2001: unresolved external symbol "private: static class singleton * singleton::mySingleton" (?mySingleton@singleton@@0PAV1@A)
The compiler is visual studio 2008, although I suspect the problem is me, not the compiler. Any suggestions what I'm doing wrong and how to fix it?
Aren't you supposed to return it as a reference or pointer? Returning it as a copy kinda defeats the whole purpose, no?
Edit, nevermind, you typedefed it. You only need one class scope operator and it goes inbetween the return variable or void and the Method name. Good luck!