有什么问题?
#include <iostream>
using namespace std;
class Singleton
{
public:
void HelloWorld();
static Singleton* Instance(){
if (instanza == 0)
instanza = new Singleton ;
return instanza;
}
protected:
Singleton();
private:
static Singleton* instanza;
};
Singleton* Singleton:: instanza = 0;
void Singleton::HelloWorld()
{
cout << "Hello World!";
}
int main()
{
Singleton *p = Singleton ::Instance();
p->HelloWorld();
delete p;
}
g ++ -Wall -o&#34; singleton&#34; &#34; singleton.cpp&#34; (nella cartella:/ home / tarek / Scrivania / Nuovi codici) /tmp/ccL8BxOT.o:nella funzione&#34; Singleton :: Instance()&#34;: singleton.cpp :(。text._ZN9Singleton8InstanceEv [_ZN9Singleton8InstanceEv] + 0x24):riferimento non definito a&#34; Singleton :: Singleton()&#34; collect2:错误:ld返回1退出状态 Compilazione fallita。
答案 0 :(得分:4)
您的构造函数声明未定义。
将其更改为
...
protected:
Singleton() {};//add further implementation here
//Singleton() = default; for c++11
...
它应该有效
答案 1 :(得分:0)
您需要将构造函数定义为
Singleton::Singleton()
{
}