2017-09-02 43 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會產生錯誤。我還沒有就是否應將任何行爲視爲問題形成意見。 –

相關問題