C++中为什么重载ifstream >>总出错?

来源:百度知道 编辑:UC知道 时间:2024/05/25 15:08:45
class POLY
{
public:
...
friend ifstream& operator >>(ifstream &, POLY &); // Overload ifstream >> (1)
...
};
ifstream& operator >>(ifstream &, POLY &) // Overload ifstream >> (2)
{
...
}
Compiling...
poly.cpp
c:\program files\exp004\poly.h(26) : error C2143: syntax error : missing ';' before '&'
c:\program files\exp004\poly.h(26) : error C2433: 'ifstream' : 'friend' not permitted on data declarations
c:\program files\exp004\poly.h(26) : error C2501: 'ifstream' : missing storage-class or type specifiers
.....
那个(1)是在>>后的,是注释.本来是想编号的....
是在哪个文件包含啊?我在'poly.cpp'中包含的.我把头文件和源文件分开的.这儿的cpp中不含main()函数

在ploy.h中包含头,注意引入名字空间.

在定义class PLOY时,已经用到了ifstream了,所以要在头文件中包含<fstream>并引入名字空间.

--------------
这段代码看起来没问题,是不是没有
#include <fstream>