Search code examples
c++operatorsistreamextraction-operator

How can I read from an std::istream (using operator>>)?


How can I read from an std::istream using operator>>?

I tried the following:

void foo(const std::istream& in) {
  std::string tmp;
  while(in >> tmp) {
     std::cout << tmp;
  }
}

But it gives an error:

error: no match for 'operator>>' in 'in >> tmp'

Solution

  • Operator >> modifies stream, so don't pass by const, just a reference.