如何从std :: istream中读取(使用运算符>>)?

时间:2009-08-19 22:31:50

标签: c++ operators istream extraction-operator

如何使用std::istreamoperator>>阅读?

我尝试了以下内容:

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

但是它给出了一个错误:

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

3 个答案:

答案 0 :(得分:9)

运营商&gt;&gt;修改流,所以不要通过const传递,只是引用。

答案 1 :(得分:3)

使用非const引用:

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

答案 2 :(得分:1)

你正在以正确的方式做到这一点。您确定要包含所需的所有标题吗? (<string><iostream>)?

相关问题