C++ 错误 C2679:二进制“<<”:未找到采用“std::string”类型的右侧操作数的运算符(或没有可接受的转换)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22724064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:10:59  来源:igfitidea点击:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

c++visual-studio-2012c++11

提问by SpikeThea

Here's my code, how do I fix this error?

这是我的代码,我该如何解决这个错误?

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
    string title = "THE WORLD OF PIRATES";
    cout << title << endl;
    cout << " Welcome to the world of pirates";

    cin.get();

    return 0;
}

The error is

错误是

binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

回答by user1942027

You forgot to #include <string>

你忘了 #include <string>

using std::stringwithout including it's header works on some compilers that indirectly import parts of <string>into their <iostream>or other headers but that's not standard and shouldn't be relied upon. Also they often break when you try to output a string since they only included a part of the implementation and are missing the part that implements the operator<<.

使用std::string不包含它的头文件适用于一些间接将部分导入<string>到它们<iostream>或其他头文件中的编译器,但这不是标准的,不应依赖。此外,当您尝试输出字符串时,它们经常会中断,因为它们只包含实现的一部分,而缺少实现operator<<.