I have a class called parser
:
class parser {
const std::istream& stream;
public:
parser(const std::istream& stream_) : stream(stream_) {}
~parser() {}
void parse();
};
In parser::parse
I want to loop over each line, so I use std::getline
:
std::getline(stream, line)
The compiler gives me this error, however:
src/parser.cc:10:7: error: no matching function for call to 'getline' \n std::getline(stream, line); \n ^~~~~~~~~~~~
But the first argument to std::getline
is of type std::istream&
, right? What could I be doing wrong?
The first argument to getline
is of type istream&
, not istream const &
. (Reading from a stream changes its state.) Take the const
qualifier off your parser::stream
member.
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.