design patterns - C++ Singleton usage: compiler complains about private constructor -


i know there million questions , answers singletons out there can't seem find solution this. running risk of negative votes, here's problem:

i want use singleton implementation andrei alexandrescu' modern c++ design:

header:

class singleton {     static singleton& instance();   private:     singleton(){};     singleton(const singleton&){};     singleton& operator=(const singleton&){};     ~singleton(){}; }; 

implementation:

#include "s.hh"  singleton& singleton::instance() {     static singleton instance;     return instance; } 

test:

#include "s.hh"  int main(void) {     singleton& single = singleton::instance();     return 0; } 

now,

$g++ a.cc s.cc  && ./a.out  in file included a.cc:1:0: s.hh: in function ‘int main()’: s.hh:3:19: error: ‘static singleton& singleton::instance()’ private  static singleton& instance();                ^ a.cc:6:42: error: within context   singleton& single = singleton::instance();                                       ^ 

what wrong that? stuck...

by default, class' members private. access singleton, need make singleton::instance public:

class singleton {   // here!   public:     static singleton& instance();    private:     singleton(){};     singleton(const singleton&){};     singleton& operator=(const singleton&){};     ~singleton(){}; }; 

note not constructor (as said in title), it's static member function supposed return reference singleton.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -