c++ - Undefined reference to Singleton::Singleton() -


i'm trying first basic singleton example design patterns working, has me stumped.

this code compiles cleanly g++ -c singleton.cpp:

class singleton { public:     static singleton* instance(); protected:     singleton(); private:     static singleton* _instance; };  singleton* singleton::_instance = 0;  singleton* singleton::instance() {     if (_instance == 0) {         _instance = new singleton;     }     return _instance; } 

but when add skeletal main() , compile g++ singleton.cpp undefined reference 'singleton::singleton()'.

what missing?

you never added definition

singleton(); 

which used in singleton* singleton::instance() by

_instance = new singleton; 

typically should can layout singleton like:

class singleton { public:     static singleton* instance() { static singleton s; return &s; }     singleton(const singleton&) = delete;     void operator=(const singleton&) = delete; private:     singleton() = default; }; 

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -