I have a class that I have been given some error input handling for. It's a class that has been given it's own "extraction" operator and I've been asked to implement the code that I've been given. The problem I'm having is that the code I should use looks similar to this.
try {
while (some condition)
{....implemented code....}
} catch (runtime_error& e) {
cerr << e.what() << endl;
return 1;
}
The problem I am having compiling this is it doesn't seem to like the "return 1" value it gives me an error of:
invalid initialization of non-const reference of type ‘std::istream& {aka std::basic_istream<char>&}’ from an rvalue of type ‘int’
If I remove the return value it compiles straight but then the program fails to execute once it hits the area where it is trying to do the try statement. As mentioned the code I have there is the above is the example code we are supposed to implement so I assumed straight out of the box it would work. My condition for the while loop was
while (!std::cin.fail())
as I assumed I'd want to keep getting input until it fails for some reason. Why would the return values in this case be causing a problem?
istream &. And I mean the reference, not a variable going out of scope.while (std::cin)- it has same meaning as the above, but 8 characters less to type.