郁闷坏了,为什么编译器总是冤枉俺捏?

laazzy 2004-01-04 03:53:01
本来这是一个很小的类,只是想用它来管理一个打开的文件
可是不知为什么编译器给出了一大堆“莫须有”的错误--我冤啊。。。
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 ***


...全文
111 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
laazzy 2004-01-04
  • 打赏
  • 举报
回复
多谢各位了,看来还是我学的不够,^_^
xiaocai365 2004-01-04
  • 打赏
  • 举报
回复
//fopener.h:

#include <cstdio>
#include<string>

using namespace std;

#ifndef __fopener_h__
#define __fopener_h__
/////////////////////////////////////
//as expection class
class bad_file
{
public:
bad_file(const string& fnm):fname(fnm) { }
bad_file(bad_file &one):fname(one.fname){ }
const string & getfname(void) const {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)const; //retrun current file pointer
long len(void) const; // 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) const; // 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
try
{
fp = fopen(fnmstr,mode_str[mod]);
if( !fp )
throw bad_file(fnmstr); // 可以自动转换
}
catch(bad_file& e)
{
cout<<"Can NOT open the file \""<<e.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);
}

try
{
fp = fopen(fnmstr,mode_str[mod]);
if( !fp )
throw bad_file(fnmstr); //我也很郁闷
}

catch(bad_file& e)
{
cout<<"Can NOT open the file \""<<e.getfname()<<"\""<<"Please make sure you have enough disk space,and you can access the file"<<endl;
throw;
}
return 1; // 返回值
}

FILE * fopener::getfp(void) const
{
return fp;
}

long fopener::len(void) const
{
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;}
xiaocai365 2004-01-04
  • 打赏
  • 举报
回复
//fopener.h:

#include <cstdio>
#include<string>

//添加
using namespace std;
短歌如风 2004-01-04
  • 打赏
  • 举报
回复
用std::FILE替换掉FILE
cai114 2004-01-04
  • 打赏
  • 举报
回复
太长了,回家再看
foxmail 2004-01-04
  • 打赏
  • 举报
回复
怎么文件名字是
foper.h和foper.cpp
你不是fopener吗
另外
#include<string>
to
#include <string>
laazzy 2004-01-04
  • 打赏
  • 举报
回复
自己up 一下,急啊

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧