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

发布于 2024-08-01 22:22:16 字数 346 浏览 3 评论 0原文

如何使用 operator>>std::istream 读取内容?

我尝试了以下操作:

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

但它给出了一个错误:

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

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冬天的雪花 2024-08-08 22:22:16

运算符>> 修改流,因此不要传递 const,仅传递引用。

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

巴黎夜雨 2024-08-08 22:22:16

使用非常量引用:

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

Use a non-const reference:

void foo(std::istream& in) {
  std::string tmp;
  while(in >> tmp) {
     std::cout << tmp;
  }
}
要走就滚别墨迹 2024-08-08 22:22:16

你正在以正确的方式这样做。 您确定包含了您需要的所有标头吗? ()?

You're doing that the right way. Are you sure you included all the headers you need? (<string> and <iostream>)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文