问个C++类单实例的问题
#include <iostream>using namespace std;
class Temp {
public:
static Temp * getInstance();
static void putInstance();
private:
static Temp *instance;
};
Temp * Temp::getInstance()
{
if (!instance)
instance = new Temp();
return instance;
}
void Temp::putInstance()
{
if (instance) {
delete instance;
instance = NULL;
}
}
int main()
{
Temp *p;
p = Temp::getInstance();
return 0;
}
=====================
上面的程序报错:
/tmp/ccpL8OB4.o: In function `Temp::putInstance()':
hello.cpp:(.text+0x79): undefined reference to `Temp::instance'
hello.cpp:(.text+0x82): undefined reference to `Temp::instance'
hello.cpp:(.text+0x90): undefined reference to `Temp::instance'
/tmp/ccpL8OB4.o: In function `Temp::getInstance()':
hello.cpp:(.text+0xa1): undefined reference to `Temp::instance'
hello.cpp:(.text+0xb6): undefined reference to `Temp::instance'
/tmp/ccpL8OB4.o:hello.cpp:(.text+0xbb): more undefined references to `Temp::instance' follow
collect2: ld returned 1 exit status
请问是什么原因啊,我不知道哪里错了,请帮助