user142019
user142019

Reputation:

no matching function for call to 'getline'

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'
    std::getline(stream, line);
    ^~~~~~~~~~~~

But the first argument to std::getline is of type std::istream&, right? What could I be doing wrong?

Upvotes: 1

Views: 1172

Answers (1)

Fred Foo
Fred Foo

Reputation: 363807

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.

Upvotes: 10

Related Questions