郁闷坏了,为什么编译器总是冤枉俺捏?
本来这是一个很小的类,只是想用它来管理一个打开的文件
可是不知为什么编译器给出了一大堆“莫须有”的错误--我冤啊。。。
winxp + vc6.0
winxp + bcc55
下边是win98 + dev-cpp 4.9.80给出的错误:
//fopener.h:
#include <cstdio>
#include<string>
#ifndef __fopener_h__
#define __fopener_h__
/////////////////////////////////////
//as expection class
class bad_file
{
public:
bad_file(string fnm){fname = fnm;}
bad_file(bad_file &one){fname = one.fname;}
string & getfname(void){return fname; }
private:
string fname;
};
////////////////////////////////////
enum open_mode {rt,wt,at,rb,wb,ab,rtplus,wtplus,atplus,rbplus,wbplus,abplus};
const char *mode_str[]={"rt","wt","at","rb","wb","ab","rt+","wt+","at+","rb+","wb+","ab+"};
class fopener
{
public:
fopener(char *fnmstr); //open as default mode : "rb"
fopener(char *fnmstr,enum open_mode mod);
~fopener();
int chgfle(char *fnmstr,enum open_mode mod);
FILE *getfp(void); //retrun current file pointer
long len(void); // return the length of the file
long read(long bytes); //read the bytes size to the tmp
long write(FILE *fpout,long types); //write to the file fp with bytes bytes
string & getfname(void); // get current file name
private:
string fname;
FILE *fp;
long flen;
long tmp_size;
char *tmp;
};
#endif
//fopener.cpp
#include "fopener.h"
#include <iostream>
#include <cstdio>
#include <cassert>
#ifndef __FOPENER_CPP__
#define __FOPENER_CPP__
using namespace std;
fopener::fopener(char *fnmstr,enum open_mode mod)
{
assert(fnmstr);
tmp_size = 10240; //default temp size is 10K
try
{ tmp = new char [tmp_size]; }
catch(bad_alloc)
{
cout<<"No enough memory,and the progarm is exiting...\n";
throw; //if we can not get enough memory
} //we let the program die as a gentleman
fname = string(fnmstr);
try
{
fp = fopen(fnmstr,mode_str[mod]);
if( !fp )
throw (bad_file(fname));
}
catch(bad_file)
{
cout<<"Can NOT open the file \""<<bad_file.getfname()<<"\""<<"Please make sure you have enough disk space,and you can access the file"<<endl;
throw;
}
fseek(fp,0,2); //get the length of the file
flen=ftell(fp);
rewind(fp);
}
fopener::~fopener()
{
if(tmp)
{
delete []tmp;
}
if(fp)
{
fclose(fp);
}
}
int fopener::chgfle(char *fnmstr,enum open_mode mod)
{
assert(fnmstr);
if(fp)
{
fclose(fp);
}
fname = string(fnmstr);
try
{
fp = fopen(fnmstr,mode_str[mod]);
if( !fp )
throw (bad_file(fname));
}
catch(bad_file)
{
cout<<"Can NOT open the file \""<<bad_file.getfname()<<"\""<<"Please make sure you have enough disk space,and you can access the file"<<endl;
throw;
}
}
FILE * fopener::getfp(void)
{
return fp;
}
long fopener::len(void)
{
return flen;
}
long fopener::read(long bytes)
{
long read_sz;
if(bytes > tmp_size) // There is no enough tmp ,let's enlarge it
{
delete []tmp;
tmp = NULL;
try
{ tmp = new char [bytes]; }
catch(bad_alloc)
{
cout<<"now enough memory,and the progarm is exiting...\n";
throw;
}
tmp_size = bytes;
}
read_sz = fread(tmp,bytes,1,fp);
return read_sz;
}
long fopener::write(FILE *fpout,long bytes)
{
long wt_sz;
assert(fpout);
if( bytes > tmp_size) // here ,we can't read a memory area larger than what we have
{
return -1;
}
wt_sz = fwrite(tmp,bytes,1,fpout);
return wt_sz;
}
#endif
int main(void)
{ return 0;}
编译器: Default compiler
执行 g++.exe...
g++.exe "C:\FOPER.CPP" -o "C:\FOPER.exe" -I"C:\DEV-CPP\include\c++" -I"C:\DEV-CPP\include\c++\mingw32" -I"C:\DEV-CPP\include\c++\backward" -I"C:\DEV-CPP\include" -L"C:\DEV-CPP\lib"
In file included from C:/FOPER.CPP:3:
C:/foper.h:11: parse error before `fnm'C:/foper.h:12: ISO C++ forbids defining types within return type
C:/foper.h:12: semicolon missing after declaration of `class bad_file'
C:/foper.h: In function `int bad_file(bad_file&)':
C:/foper.h:12: `fname' undeclared (first use this function)
C:/foper.h:12: (Each undeclared identifier is reported only once for each
function it appears in.)
C:/foper.h:12: `class bad_file' has no member named `fname'C:/foper.h: At global scope:
C:/foper.h:13: syntax error before `&' tokenC:/foper.h:33: syntax error before `&' token
C:/foper.h:36: 'string' is used as a type, but is not defined as a type.C:/FOPER.CPP: In constructor `fopener::fopener(char*, open_mode)':
C:/FOPER.CPP:35: parse error before `)' token
C:/FOPER.CPP: At global scope:
C:/FOPER.CPP:41: `fp' was not declared in this scope
C:/FOPER.CPP:41: ISO C++ forbids declaration of `fseek' with no type
C:/FOPER.CPP:41: `int fseek' redeclared as different kind of symbol
C:/DEV-CPP/include/stdio.h:262: previous declaration of `int fseek(FILE*, long
int, int)'
C:/FOPER.CPP:41: initializer list being treated as compound expression
C:/FOPER.CPP:42: ISO C++ forbids declaration of `flen' with no type
C:/FOPER.CPP:42: `fp' was not declared in this scope
C:/FOPER.CPP:43: `fp' was not declared in this scope
C:/FOPER.CPP:43: ISO C++ forbids declaration of `rewind' with no type
C:/FOPER.CPP:43: `int rewind' redeclared as different kind of symbol
C:/DEV-CPP/include/stdio.h:264: previous declaration of `void rewind(FILE*)'
C:/FOPER.CPP:45: parse error before `}' tokenC:/FOPER.CPP: In member function `int fopener::chgfle(char*, open_mode)':
C:/FOPER.CPP:75: parse error before `)' token
C:/FOPER.CPP: At global scope:
C:/FOPER.CPP:80: parse error before `}' token执行结束
bcc55:
Error E2238 foper.h 37: Multiple declaration for 'fopener::FILE'
Error E2344 foper.h 29: Earlier declaration of 'fopener::FILE'
Error E2139 foper.h 37: Declaration missing ;
Error E2451 FOPER.CPP 27: Undefined symbol 'fname' in function fopener::fopener(
char *,open_mode)
Error E2314 FOPER.CPP 27: Call of nonfunction in function fopener::fopener(char
*,open_mode)
Error E2451 FOPER.CPP 30: Undefined symbol 'fp' in function fopener::fopener(cha
r *,open_mode)
Error E2108 FOPER.CPP 37: Improper use of typedef 'bad_file' in function fopener
::fopener(char *,open_mode)
Warning W8057 FOPER.CPP 45: Parameter 'mod' is never used in function fopener::f
opener(char *,open_mode)
Error E2451 FOPER.CPP 53: Undefined symbol 'fp' in function fopener::~fopener()
Error E2451 FOPER.CPP 62: Undefined symbol 'fp' in function fopener::chgfle(char
*,open_mode)
Error E2451 FOPER.CPP 67: Undefined symbol 'fname' in function fopener::chgfle(c
har *,open_mode)
Error E2314 FOPER.CPP 67: Call of nonfunction in function fopener::chgfle(char *
,open_mode)
Error E2228 FOPER.CPP 67: Too many error or warning messages in function fopener
::chgfle(char *,open_mode)
*** 26 errors in Compile ***