2016-09-01 3 views
-8

Was ist das Problem?Fehler Singleton in C++

#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 "Singletons" "singleton.cpp" (im Ordner:/home/tarek/Desktop/New Codes) /tmp/ccL8BxOT.o: in der Funktion „Singleton :: Instanz () ":. singleton.cpp :(text._ZN9Singleton8InstanceEv [_ZN9Singleton8InstanceEv] + 0x24): undefined Verweis auf" Singleton :: Singleton() „ collect2: Fehler: ld ergab 1 Exit-Status Compilation fehlgeschlagen.

+5

Zeigen Sie uns Inglese Fehler. Gerade 'LANG = C' vor dem g ++ Befehl. – Nidhoegger

+1

Die Botschaft ist ziemlich klar: 'Singleton :: Singleton()' ist nicht definiert. – molbdnilo

+1

* Was ist das Problem? * Ist das, was Sie berichten. Stackoverflow ist nicht Remote-Debugging-Dienst. Und Ihre Singleton Idiom ist gebrochen - was ist, wenn 'Instance' genannt wird before' = 0 'Instanz? Verwenden Sie lokale statische Variablen für Singletons. –

Antwort

4

Ihr Konstruktor wird erklärt einfach nicht definiert.

Änderung es

... 
protected: 
     Singleton() {};//add further implementation here 
     //Singleton() = default; for c++11 
... 

und es sollte

+0

die Informationen Vielen Dank für – Tarek

0

Sie müssten arbeiten definieren Konstruktor als

Singleton::Singleton() 
{ 
}