类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:
我做了一个全局变量的头文件和定义文件;
// global.h
#include <fstream.h>
extern ofstream OutFile;
//global.cpp
ofstream OutFile("apm.dat");
然后在其他文件中包含global.h
编译都通过
不过在运行的时候会出现非法访问;
调试了一哈;好象是OutFile还没有定义;
这是怎么会事情呢?怎么解决哦?
网友回答:
因为基于对象的编程,构造都是有域的,所以基于对象是没有全局变量的。不过可以使用static变量!
to aganpro(阿甘)
那这个ofstream 对象这么用指针来做呢?
-----------------------------
// global.h
#include <fstream.h>
ofstream *OutFile;
调用处:
OutFile = new ofstream("Out.txt");
*OutFile << "..";
//OutFile->write(...);
delete OutFile;