2012-01-31 19 views
0
#include<iostream> 
#include<vector> 
std::vector<std::string> vector1; 
int main() { 
    vector1.push_back("adadad"); 
    std::vector<std::string> vector2; 
    vector2.push_back("adadd"); 
    if (vector1==vector2) { 
     std::cout<<"success"; 
    } else { 
     vector1.swap(vector2); 
     vector2.clear(); 
     vector2.push_back("adadd"); 
     if (vector1==vector2) { 
      std::cout<<"success_swap"; 
     } 
    } 
} 

現在這個工作在g ++中,但不在visual studio中。運算符==在這裏不起作用,並在Visual Studio 2010(最終)中引發編譯錯誤。 如果向量是整數類型,則同樣可行。我在這裏丟失了什麼?這不是一個linux本地的東西,他們已經省略了。爲什麼在gcc中有一個實現,但不在vC++中?矢量等於類型<std::string>在g ++中工作,但不在visual studio 2010中

,它顯示的錯誤信息是:

[snip]\vc\include\xutility(2990): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion) 
      with 
      [ 
       _Elem=char, 
       _Traits=std::char_traits<char>, 
       _Ax=std::allocator<char> 
      ] 
      [snip]\vc\include\exception(470): could be 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)' 
      [snip]\vc\include\exception(475): or  'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)' 
      [snip]\vc\include\exception(481): or  'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)' 
      [snip]\vc\include\system_error(408): or  'bool std::operator ==(const std::error_code &,const std::error_condition &)' 
      [snip]\vc\include\system_error(416): or  'bool std::operator ==(const std::error_condition &,const std::error_code &)' 
      while trying to match the argument list '(const std::basic_string<_Elem,_Traits,_Ax>, const std::basic_string<_Elem,_Traits,_Ax>)' 
      with 
      [ 
       _Elem=char, 
       _Traits=std::char_traits<char>, 
       _Ax=std::allocator<char> 
      ] 
      [snip]\vc\include\xutility(3030) : see reference to function template instantiation 'bool std::_Equal<_InIt1,_InIt2>(_InIt1,_InIt1,_InIt2)' being compiled 
      with 
      [ 
       _InIt1=const std::basic_string<char,std::char_traits<char>,std::allocator<char>> *, 
       _InIt2=std::_Vector_const_iterator<std::_Vector_val<std::string,std::allocator<std::string>>> 
      ] 
      [snip]\vc\include\xutility(3051) : see reference to function template instantiation 'bool std::_Equal1<const std::basic_string<_Elem,_Traits,_Ax>*,_InIt2>(_InIt1,_InIt1,_InIt2,std::tr1::true_type)' being compiled 
      with 
      [ 
       _Elem=char, 
       _Traits=std::char_traits<char>, 
       _Ax=std::allocator<char>, 
       _InIt2=std::_Vector_const_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>, 
       _InIt1=const std::basic_string<char,std::char_traits<char>,std::allocator<char>> * 
      ] 
      [snip]\vc\include\vector(1489) : see reference to function template instantiation 'bool std::equal<std::_Vector_const_iterator<_Myvec>,std::_Vector_const_iterator<_Myvec>>(_InIt1,_InIt1,_InIt2)' being compiled 
      with 
      [ 
       _Myvec=std::_Vector_val<std::string,std::allocator<std::string>>, 
       _InIt1=std::_Vector_const_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>, 
       _InIt2=std::_Vector_const_iterator<std::_Vector_val<std::string,std::allocator<std::string>>> 
      ] 
      [snip]\test\main.cpp(8) : see reference to function template instantiation 'bool std::operator ==<std::string,std::allocator<_Ty>>(const std::vector<_Ty> &,const std::vector<_Ty> &)' being compiled 
      with 
      [ 
       _Ty=std::string 
      ] 
+0

我確認錯誤發生在visual studio – 2012-01-31 17:44:50

+0

將來,請從「輸出」窗口發佈完整的錯誤消息。不是「錯誤」窗口,雖然即使那樣會比沒有更好 – 2012-01-31 17:51:19

+0

@MooingDuck對此感到抱歉。我預計在所有版本的Visual Studio中出現錯誤。請記住 – King 2012-01-31 18:00:55

回答

1

錯誤發生,因爲MSVC,std::string的平等運算符(==)是而不是由<iostream><vector>包含的。您還必須包含<string>

該消息的關鍵是: 「錯誤C2678:二進制'==':找不到操作符找到類型爲'const std :: basic_string < _Elem,_Traits,_Ax>'(或沒有可接受的轉換)的左操作數」

+0

有一點,字符串實際上並不是標準類型中的內置數據類型,而是單獨的類,因此我們需要它。但爲什麼g ++首先接受它? g ++中的這個實現在哪裏?這種不同的實現背後的原因是什麼? – King 2012-02-02 22:25:14

+0

基本上,iostream或者vector頭文件都包含了包含basic_string的文件,但不包括。我不知道哪個。它可能是像iostream包含例外,其中包括basic_strings或東西。 – 2012-02-02 22:32:56

0

你可能使用一個很老的編譯器。

這在2005年MSVS,這是不是新的本身對我的作品。

如果你的編譯器是符合C++03,它應該工作:

23.2.4

template <class T, class Allocator> 
bool operator == (const vector<T,Allocator>& x, 
        const vector<T,Allocator>& y); 
+0

我正在使用Microsoft Visual Studio 2010最終版!奇怪的是,你說它在2005年本身有效。它適用於整型類型,但不適用於我的字符串。奇怪..怪物! – King 2012-01-31 17:10:50

相關問題