1

I have the following code:

#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int main() {
    // your code goes here
    stringstream instream("a x b c d x c d");
    string line;
    bool loop;
    loop = getline(instream, line);
    return 0;
}

It works on gcc, but when I compile using Visual Studio 2013 I get the build error at loop = getline(instream, line):

cannot convert from 'std::basic_istream<char, std::char_traits<char> > to 'bool'

How do I fix this problem?

Dzung Nguyen
  • 3,794
  • 9
  • 48
  • 86

1 Answers1

2

Implicit boolean conversion rules changed from C++03 to C++11, and compiler support for this stuff varies hugely across platforms.

If you really need this, I'd use the old force-boolean trick:

loop = !!getline(instream, line);

You don't need the trick if you plonk getline into an if conditional directly, instead of first assigning it to a variable, because if is special.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055