Singleton.h
class Singleton{ public: static Singleton* getInstance(){ if(instance == NULL){ instance = new Singleton(); } return instance; } private: static Singleton * instance; Singleton(){}; }; Singleton* Singleton::instance = NULL;
main.cpp
int main(){ Singleton * instance = NULL; instance = Singleton::getInstance(); return 0; }
原因在于仅声明了Singleton类的静态变量instance,但缺少变量定义,造成“无法解析的符号”错误。
时间: 2024-10-05 03:56:47