このJavaシングルトンクラスをC++に移植しようとしています。
public class Singleton {
private static Singleton uniqueInstance;
private Singleton() {}
public static Singleton getInstance() {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
}
私はこのC++コードに移植しました:
class Singleton {
private:
Singleton() {
cout << "new Singleton is called" << endl;
}
static Singleton* uniqueInstance;
public:
static Singleton* getInstance() {
if (!uniqueInstance) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
};
しかし、私はこれをコンパイルすることはできません!およびgccリンカーエラーが発生しました。