c++ - 错误 C2679: "binary ' <<': no operator found which takes a right-hand operand of type ' std::string'(或没有可接受的转换)”

标签 c++ windows visual-studio

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

我认为应该这样写:'cout << "I'm"<< emotion[x] << endl;',我确信这在过去对我有用,但出于某种原因电脑不会接受的。我实际上已经有一段时间没有用 C++ 编写代码了,所以可能有些地方发生了变化,或者我忘记了“cout”是如何工作的。

这可能是一些 super 简单和愚蠢的东西,我应该已经捕获了,但我就是看不到它。这是其余代码,以防问题出在更远的地方。

#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");
}

最佳答案

正如您所想,确实是一个简单的错误。你需要#include <string>使其按预期工作并且Here Is Why .

您可能还需要 #include <cstdlib>对于 srandrandHere is why you may not need it .

关于c++ - 错误 C2679: "binary ' <<': no operator found which takes a right-hand operand of type ' std::string'(或没有可接受的转换)”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38909757/

相关文章:

c++ - Eclipse 和 Gtkmm - "undefined reference to"

c++ - 在 STL 容器中存储 OpenCV Mat 对象时避免内存泄漏

python - 如何将一些字符串粘贴到 Python 中的事件窗口?

windows - 挂载 *.zips 之类的存档(在没有驱动程序的情况下在内核模式下工作)

visual-studio - 使用单个输出文件创建安装项目 (Visual Studio 2005)

C# 添加新子对象时更新父对象

c++ - 在 C++ 中将缓冲区导出为 WAV

c++ - 重载成员函数指针的转换助手

windows - 如何在 Batch 中回显 > 字符

c++ - 反汇编程序 Visual Studio C++