【问题标题】:Error: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' [duplicate]错误:二进制“<<”:未找到采用“std::string”类型的右侧操作数的运算符 [重复]
【发布时间】:2013-11-09 04:20:41
【问题描述】:

我的问题与其他问题相似,但我无法找到并回答非常合适的问题,也许我只是想念它,但无论如何。

鉴于这是我的 .cpp 的顶部:

#include <cstring>

#include <iostream>

using namespace std

为什么这行会有错误:

cout << endl << output << endl;

错误是:

二进制 '

【问题讨论】:

  • 您没有包含正确的标题。不过,我很惊讶我能够从你的问题中推断出这一点。您的标题对任何人都没有用,并且代码不完整且未格式化。
  • 提示:&lt;cstring&gt;&lt;string&gt; 是两个截然不同的东西。
  • 你从哪里学到\#include这个语法?
  • @texasbruce:那是他试图让#include 不以大粗体字母出现。于浩格式化了代码,但把斜线留在了里面。
  • 顺便说一下,@user2971135,为了将来参考,只需像在文本编辑器中一样输入代码,但要缩进 4 个额外的空格。如果您选择代码并单击如下所示的按钮:{},它将为您执行缩进。

标签: c++ string binary cout stdstring


【解决方案1】:

&lt;cstring&gt; 是 C 字符串的头部,即其内容与 C 头部 string.h 相同。你需要处理std::string的是&lt;string&gt;

另一个问题是你错过了分号:

using namespace std;
//                 ^

注意,这种风格有效,但不推荐,最好不要使用此行并使用:

std::cout << std::endl << output << std::endl;

【讨论】:

  • 或者更好,std::cout &lt;&lt; '\n' &lt;&lt; output &lt;&lt; '\n';。除了极少数例外,需要endl 意味着您的缓冲设置不正确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 2018-01-26
  • 1970-01-01
相关资源
最近更新 更多