读书人

请问VC下类静态成员编译出错的有关问题

发布时间: 2012-02-24 16:30:39 作者: rapoo

请教VC下类静态成员编译出错的问题
程序如下,请大家给指点一下
两个文件log.h和main.cpp
vc2005下编译出错
main.obj : error LNK2001: 无法解析的外部符号 "public: static class CLog * CLog::_instance" (?_instance@CLog@@2PAV1@A)

//log.h


#ifndef _CLOG_H
#define _CLOG_H
#pragma once

#include <fstream>
#include <ios>
using namespace std;

class CLog
{
ofstream *pf;

public:
static CLog* _instance;

CLog()
{
pf= new ofstream ("default.log", ios::app);
}

~CLog()
{
_instance=0;
pf->close();
}

static CLog* GetPtr()
{
if(!_instance)
_instance = new CLog;
return(_instance);
}
};
#endif
//end of file log.h


#include <stdio.h>
#include "log.h"

int main(int argc, char *argv[])
{
CLog loglog;
return 0;
}


[解决办法]
需要在类外定义一下
CLog* CLog::_instance = NULL;

读书人网 >C++

热点推荐