本文介绍了以下单例类有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! // Th: class T { public: static T * instance(); private: T(){} ~T(){} static T * smInstance; }; // T.cpp: T * T :: instance() { if(smInstance == NULL) smInstance = new T(); 返回smInstance; } 当我尝试编译上面的代码时,有链接器错误: ... / T.cpp:3:未定义引用`T :: smInstance` 我在gentoo linux下使用gcc 3.4.6,谢谢// T.h:class T{public:static T* instance();private:T() {}~T() {}static T* smInstance;};// T.cpp:T* T::instance(){if (smInstance == NULL)smInstance = new T();return smInstance;}when I try to compile the above code, there is linker error:.../T.cpp:3: undefined reference to `T::smInstance`I am using gcc 3.4.6 under gentoo linux, thanks推荐答案 你已声明但未定义''smInstance''。 它应该在你的[T.cpp]文件中定义。 但是,只需要做 class T { private: T(){} T(T const& ); ~T(){} public: static T&实例() { T theInstance; 返回实例; } }; - 答:因为它弄乱了人们通常阅读文字的顺序。 问:为什么这么糟糕? A:热门发布。 问:usenet和电子邮件中最烦人的是什么?You have declared but not defined ''smInstance''.It should be defined in your [T.cpp] file.But instead, just doclass T{private:T() {}T( T const& );~T() {}public:static T& instance(){T theInstance;return theInstance;}};--A: Because it messes up the order in which people normally read text.Q: Why is it such a bad thing?A: Top-posting.Q: What is the most annoying thing on usenet and in e-mail? 应该是 static T theInstance; 当然。 返回实例; } };Should bestatic T theInstance;of course. return theInstance; } }; - 答:因为它弄乱了人们通常阅读文字的顺序。 问:为什么这么糟糕? A :热门发布。 问:usenet和电子邮件中最烦人的事情是什么?--A: Because it messes up the order in which people normally read text.Q: Why is it such a bad thing?A: Top-posting.Q: What is the most annoying thing on usenet and in e-mail? 这篇关于以下单例类有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-09 02:14