繁体   English   中英

在if语句中时,ifstream :: eof引发类型错误

[英]ifstream::eof throws a type error when in if statement

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

class A
{
private:
   std::ifstream filestr;

public:
   int CalcA(unsigned int *top);  
}

然后在cpp文件中

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

      if((this->filestr).eof);
      {
         error = 1;
         break;
      }
   }
   return error;
}

我收到一个编译错误

error: argument of type ‘bool (std::basic_ios<char>::)()const’ does not match ‘bool’

谁能告诉我如何正确使用eof? 还是其他任何原因导致我收到此错误?

eof是一个函数 ,因此需要像其他函数一样调用: eof()

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

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

尝试

if(this->filestr).eof())

(this->filestr).eof本身就是指向成员方法的指针。 if语句需要bool类型的表达式。 因此,您需要调用该方法。 这将成功,因为它返回bool值。

(this->filestr).eof没有调用该函数。 (this->filestr).eof()是。 :-)这解释了您的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM