错误C2679:二进制'<<':找不到哪个运算符采用'std :: string'类型的右手操作数(或者没有可接受的转换)

Spi*_*hea 21 c++ c++11 visual-studio-2012

这是我的代码,如何修复此错误?

#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;
}
Run Code Online (Sandbox Code Playgroud)

错误是

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

use*_*027 52

你忘了 #include <string>

使用std::string而不包括它的标题适用于某些编译器,这些编译器间接地将部分<string>导入其<iostream>标题或其他标题中,但这不是标准的,不应该依赖它.当你尝试输出一个字符串时,它们经常会断开,因为它们只包含了一部分实现,并且缺少实现它的部分operator<<.

  • 我已经包含了<string.h>,它在vs2010中工作但在vs2015中没有.谢谢! (2认同)