2012-10-23 59 views
1

我試過的Visual C++ 2008 Express Edition的這段代碼,但它不會編譯:std :: find在std :: vector <std :: string>不能在Visual C++ 2008中編譯?

#include <iostream> 
#include <vector> 
#include <algorithm> 

int main() 
{ 
    typedef std::string Element; 
    typedef std::vector<Element> Vector; 
    typedef Vector::iterator Iterator; 

    Vector v; 
    std::find(v.begin(), v.end(), std::string("xxx")); 

    return 0; 
} 

我收到以下錯誤:

c:\programmi\microsoft visual studio 9.0\vc\include\algorithm(40) : error C2784: 'bool std::operator ==(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'std::basic_string<_Elem,_Traits,_Ax>' 

相同的代碼進行修正的GCC編譯和工作原理預期。

這是Visual Studio的錯誤嗎?我怎樣才能讓我的例子在Visual C++ 2008上工作?

+1

如果您希望人們查看錯誤,則需要輸入錯誤的輸出。除了不用任何東西來填充你的向量,只是爲了測試std :: find()? – lollancf37

回答

8

你忘了#include <string>

您必須始終包含代碼所需的所有標題。永遠不要依賴偶爾發生的魔術遞歸包含。對於您在代碼中使用的所有內容,您必須知道其聲明的位置,並確保聲明在您的翻譯單元中可見。

+2

@Daniele實際上,VS可以通過在typedef中首次使用'std :: string'而不是在某些內部庫函數中扼殺你。所以你不是一個白癡比VS編譯器本身;) –

+0

看代碼,不包括確實似乎是一個可能的問題,但它確實不符合錯誤消息輸出,它知道basic_string,因此必須是包括來自某處的字符串。爲什麼在兩個向量上嘗試運算符==? – CashCow

相關問題