2017-09-02 59 views
-2

因此,我开始编写代码,然后测试是否还记得如何投射,直到我的运算符下方出现一条红线。 这是编译器错误:插入运算符不与矢量一起工作,我不知道为什么

Error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' (or there is no acceptable conversion) (12) 

老实说,我从未有过的问题输出字符串/矢量,所以我不知道如何解决这个问题。有人可以告诉我如何解决这个问题。如果你能告诉我代码出了什么问题,那也会很棒。

#include "stdafx.h" 
#include <iostream> 
#include <vector> 
using namespace std; 

int main() 
{ 
    vector<string>hello; 
    hello.push_back("9"); 
    for (auto i : hello) 
     cout << i << " "; <-- The first operator is underlined. Why? 
    return 0; 
} 
+2

这是C++,不包含C标签! – tilz0R

+0

尝试包括'' –

+1

@JakubGaweł包含标题

回答

1

你需要一个更包括在你的程序:

#include <string> 

虽然<iostream>并声明/定义一些字符串相关的功能,不是所有的人。

对于一些编译器,iostream头内部包含字符串,但这不是标准所要求的 - 而Visual Studio不会,所以您会收到此错误。

+0

@Dutow纯粹出于兴趣,我刚刚检查并且苹果LLVM版本8.0.0(clang-800.0.42.1)编译并运行代码,但不包括。所以是的,在这种情况下VS是特定的。 –

+0

@ASMackay - 那么你可以使用'std :: string'而不包含正确的头文件,或者编译器告诉你代码出错了? –

+0

@BoPersson澄清; LLVM允许在问题中使用std :: string,但不包括正确的标题,但MSVC会产生错误。我还没有就是否应将任何行为视为问题形成意见。 –

相关问题