Tulo Stone
Tulo Stone

Reputation: 35

error: no match for operator

I do have this piece of code

    // Open the file
    infile.open(filename);
    if (!infile) {
      stockmsg << "InputData::read: Failed trying to open file " << filename << endl;
      return NULL;
    }

    // Header line confirms if right kind of file
    // In the future, might need to check the version number
    count++;
    infile >> keyword;
    if (keyword != "#STOCK") {
      stockmsg << "InputData::read : input file header line unrecognised" << endl;
      return NULL;
    }
    getline(infile,nextline,'\n');  // Discard the rest of the line

    // Read the file line by line
    etree = NULL;
    while (status == READ_SUCCESS) {
      count++;

// +++++++++++++++
// KEYWORDS stage
// +++++++++++++++

      // When in KEY_WORDS mode, try to get another line
      if (stage == KEY_WORDS) {
        if (getline(infile,nextline,'\n') == 0) {
          stockmsg << "InputData::read : unexpected end of file at line " << count << endl;
          status = READ_FAILURE;
          break;
        }

When I compile, I get an error message

error: no match for 'operator==' 
(operand types are 'std::basicistream<char>' and 'int')
if (getline(infile,nextline,'\n')==0) {

I am not sure how to fix this.

Upvotes: 1

Views: 300

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385098

It's what it says.

You did a getline then you attempt to compare the result (which is the stream) to 0.

That doesn't work. A stream and an integer can't be compared with one another. However, there is magic between streams and booleans that you can use.

So, write this:

if (!getline(infile,nextline,'\n')) {

The getline expression will be "truthy" when the stream state is good.

(I'm somewhat simplifying how the stream's booleaniness works, but this'll do for now.)

Upvotes: 4

Related Questions