简体   繁体   中英

ifstream::eof throws a type error when in if statement

I have a class A that has a std::ifstream filestr member. In one of the class functions I test to see if the stream has reached eof.

class A
{
private:
   std::ifstream filestr;

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

Then in the cpp file I have

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

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

I get a compile error

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

Can anyone tell me how to properly use eof? Or any other reasons why I get this error?

eof is a function , so it needs to called like other functions: eof() .

That said, the reading loop given can be written more correctly (taking into account other possibilities for failure other than end-of-file) without a call to eof() , but turning the read operation into the loop condition:

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

Try

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

(this->filestr).eof alone is a pointer to member method. if statements requires exprensions of type bool . So you need to call the method. That will succeed because it returns a bool value.

(this->filestr).eof is not calling the function. (this->filestr).eof() is. :-) This explains your error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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