シングルトンについて何百万もの質問と回答があることは知っていますが、これに対する解決策を見つけることができないようです. したがって、反対票のリスクを冒すと、ここに私の問題があります:
Andrei Alexandrescu の Modern C++ Design からこのシングルトン実装を使用したいと思います。
ヘッダ:
class Singleton
{
static Singleton& Instance();
private:
Singleton(){};
Singleton(const Singleton&){};
Singleton& operator=(const Singleton&){};
~Singleton(){};
};
実装:
#include "s.hh"
Singleton& Singleton::Instance()
{
static Singleton instance;
return instance;
}
テスト:
#include "s.hh"
int main(void)
{
Singleton& single = Singleton::Instance();
return 0;
}
今、
$g++ A.cc s.cc && ./a.out
In file included from A.cc:1:0:
s.hh: In function ‘int main()’:
s.hh:3:19: error: ‘static Singleton& Singleton::Instance()’ is private
static Singleton& Instance();
^
A.cc:6:42: error: within this context
Singleton& single = Singleton::Instance();
^
それの何が問題なのですか?ハマった...