ifstream :: eof在if语句中抛出类型错误

Cal*_*orm 1 c++ ifstream

我有一个A类,它有一个std :: ifstream filestr成员.在其中一个类函数中,我测试以查看流是否已达到eof.

class A
{
private:
   std::ifstream filestr;

public:
   int CalcA(unsigned int *top);  
}
Run Code Online (Sandbox Code Playgroud)

然后在我的cpp文件中

int CalcA(unsigned int *top)
{
   int error;
   while(true)
   {
      (this->filestr).read(buffer, bufLength);

      if((this->filestr).eof);
      {
         error = 1;
         break;
      }
   }
   return error;
}
Run Code Online (Sandbox Code Playgroud)

我收到编译错误

error: argument of type ‘bool (std::basic_ios<char>::)()const’ does not match ‘bool’
Run Code Online (Sandbox Code Playgroud)

谁能告诉我如何正确使用eof?或者我收到此错误的任何其他原因?

R. *_*des 6

eof是一个函数,所以它需要像其他函数一样调用:eof().

也就是说,给定的读取循环可以更准确地写入(考虑到除文件结尾之外的其他失败可能性)而无需调用eof(),但将读取操作转换为循环条件:

while(filestr.read(buffer, bufLength)) {
    // I hope there's more to this :)
};
Run Code Online (Sandbox Code Playgroud)