标题:问个C++类单实例的问题
取消只看楼主
blues1207
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-3-18
结帖率:0
 问题点数:0 回复次数:0 
问个C++类单实例的问题
#include <iostream>
using namespace std;
class Temp {
        public:
                static Temp * getInstance();
                static void putInstance();
        private:
                static Temp *instance;
};
Temp * Temp::getInstance()
{
        if (!instance)
                instance = new Temp();
        return instance;
}
void Temp::putInstance()
{
        if (instance) {
                delete instance;
                instance = NULL;
        }  
}
int main()
{
        Temp *p;
        p = Temp::getInstance();
        return 0;
}

=====================
上面的程序报错:
/tmp/ccpL8OB4.o: In function `Temp::putInstance()':
hello.cpp:(.text+0x79): undefined reference to `Temp::instance'
hello.cpp:(.text+0x82): undefined reference to `Temp::instance'
hello.cpp:(.text+0x90): undefined reference to `Temp::instance'
/tmp/ccpL8OB4.o: In function `Temp::getInstance()':
hello.cpp:(.text+0xa1): undefined reference to `Temp::instance'
hello.cpp:(.text+0xb6): undefined reference to `Temp::instance'
/tmp/ccpL8OB4.o:hello.cpp:(.text+0xbb): more undefined references to `Temp::instance' follow
collect2: ld returned 1 exit status

请问是什么原因啊,我不知道哪里错了,请帮助
搜索更多相关主题的帖子: 单实例 
2010-03-18 10:17



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-299775-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.529960 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved