lundi 8 novembre 2021

is there something wrong with my c++ singleton design pattern code? [duplicate]

using namespace std;
class singleton_l  //lazy
{  
    private:  
        singleton_l(){cout << "lazy singleton created" << endl;}    
        ~singleton_l(){}
        static singleton_l* Instance;  
    public:  
        static singleton_l* GetInstance()  
        {  
            if (Instance == nullptr)
                Instance = new singleton_l();  
            return Instance;  
        }  
        void show()
        {
            cout << "I'm a instance of singleton_l" << endl;
        }
}; 


int main() {

    singleton_l * s1; 
    s1->GetInstance();  //use singleton::GetInstance() will have the same compile error
    s1->show();

    return 0;
}

when I compile it with 'g++ test_singleton.cpp', the output error is below:

Undefined symbols for architecture arm64: "singleton_l::Instance", referenced from: singleton_l::GetInstance() in test_singleton-00665a.o ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Aucun commentaire:

Enregistrer un commentaire