请教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;