我試圖端口這個Java單例類C++: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鏈接器錯誤發生。
請準確顯示鏈接器錯誤是什麼。 – seand 2013-03-24 08:04:00