【问题标题】:Error C2679: "binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)" [duplicate]错误 C2679:“二进制 '<<':未找到采用 'std::string' 类型的右侧操作数的运算符(或没有可接受的转换)” [重复]
【发布时间】:2016-12-18 23:36:09
【问题描述】:

我的弟弟目前正在学习如何用 C++ 编写代码,为了测试数组的不同使用方式,他启动了一个程序,让计算机生成一个随机数,然后输出特定的情绪(存储在一个数组)基于该数字。但是由于某种原因,输出所在的代码部分不想按照我建议的方式工作。

我认为应该写的方式是'cout

这可能是我应该已经抓住的超级简单和愚蠢的东西,但我就是看不到它。这是其余的代码,以防问题出在更远的地方。

#include <iostream>
#include <ctime>
#include <fstream>

using namespace std;


string emotion[6];

int main() {
        emotion[0] = "Happy";
        emotion[1] = "Sad";
        emotion[2] = "Angry";
        emotion[3] = "Fearful";
        emotion[4] = "Disgusted";
        emotion[5] = "Suprised";

        srand(time(NULL));

        int x;
        x = (rand() % 6);

        cout << "I'm " << emotion[x] << endl;

        system("PAUSE");
}

【问题讨论】:

  • “电脑不接受”是什么意思?编译器错误?运行时错误?意外行为?
  • 在你的程序中添加 cstdlib。它会起作用的。你能说出你想要什么输出吗?

标签: c++ windows visual-studio


【解决方案1】:

正如您所想,确实是一个简单的错误。您需要#include &lt;string&gt; 才能使其按预期工作,Here Is Why。

对于srand 和rand 和Here is why you may not need it,您可能还需要#include &lt;cstdlib&gt;。

【讨论】:

  • facepalm 我完全忘记了#include 。谢谢。
  • 很高兴我们能够提供帮助 :) 如果它回答了您的问题,请接受/支持我的回答。
猜你喜欢
  • 1970-01-01
  • 2018-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多