低于我代码不为comiling单例模式C++单例模式_实现与存储器managenet
(错误LNK2019:解析外部符号 “私人:__thiscall辛格尔顿::辛格尔顿(无效)”(?? 0Singleton @@ AAE @ XZ)在函数“public:static class Singleton * __cdecl Singleton :: returnOneInstance(void)”(?returnOneInstance @ Singleton @@ SAPAV1 @ XZ)中引用)
任何人都可以帮忙吗?我也想知道如何管理内存?谢谢
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
class Singleton
{
private:
Singleton();
Singleton(const Singleton& sing);
void operator=(const Singleton& sing);
static Singleton* singleton;
/*The 'instance' field holds the reference of the one and only instance.
It is stored in a static variable because its scope has to be the class itself and not a particular instance.
*/
public:
static Singleton* returnOneInstance();
void printme() const;
};
Singleton* Singleton::singleton=NULL;
Singleton* Singleton::returnOneInstance(){
if (!singleton){
singleton=new Singleton;
}
return singleton;
};
void Singleton::printme()const {
cout << "I'm the Singleton" << endl;
}
int main()
{
Singleton* m=Singleton::returnOneInstance();
m->printme();
system("PAUSE");
return 0;
}
是,其中*是*了'辛格尔顿()'定义? –
此外,不要忘记阅读[什么是单身人士如此糟糕?](http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons?rq=1) –