首页  >  文章  >  后端开发  >  为什么我不能使用 `cout` 输出字符串?

为什么我不能使用 `cout` 输出字符串?

Patricia Arquette
Patricia Arquette原创
2024-10-27 08:03:30564浏览

Why can't I output strings using `cout`?

解决cout无法输出字符串的问题

为什么cout无法输出字符串?让我们深入研究这个问题并提供解决方案。

在您的代码片段中:

<code class="cpp">string text;
text = WordList[i].substr(0, 20);
cout << "String is  : " << text << endl;

您将遇到以下错误:

Error 2   error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)  c:\users\mollasadra\documents\visual studio 2008\projects\barnamec\barnamec\barnamec.cpp    67  barnamec**

解决此错误,您必须包含必要的标头:

<code class="cpp">#include <string>
#include <iostream></code>

标头包含 std::string 类的声明,而 则包含 std::string 类的声明。标头包含 std::cout 对象的定义和

这是更正后的代码:

<code class="cpp">#include <string>
#include <iostream>

string text;
text = WordList[i].substr(0, 20);
cout << "String is  : " << text << endl;</code>

现在,您应该能够使用 cout 成功输出字符串。

以上是为什么我不能使用 `cout` 输出字符串?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn