2012-01-31 11 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(最終版)にコンパイルエラーがスローされます。 ベクトルが整数型の場合は同じです .Am私は何かが不足していますか?これは省略したLinux固有のものではありません。なぜgccには実装がありますが、vC++には実装されていませんか?それが示しベクトルはタイプ<std::string>と同じですが、g ++では動作しますがビジュアルスタジオ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

ビジュアルスタジオ –

+0

のエラーが発生します。今後、「出力」ウィンドウから完全なエラーメッセージを投稿してください。 「エラー」ウィンドウではなく、それでも何もないよりも良いでしょう。 –

+0

@MooingDuckそれは残念です。私はVisual Studioのすべてのバージョンでエラーが予想されました。 – King

答えて

1

MSVCで、std::stringの等価演算子(==)がであるため、エラーが発生しました<iostream>または<vector>に含まれるではありません。 <string>も含める必要があります。

メッセージのキー行は次のとおりです。 "エラーC2678:バイナリ '==': 'const std :: basic_string型の左辺オペランドをとる演算子が見つかりません< _Elem、_Traits、_Ax>'(または受け入れ可能な変換はありません)"

+0

文字列は、別のクラスではなく標準の型の組み込みデータ型として実際には来ないという点があります。しかし、なぜg ++が最初にそれを受け入れるのですか? g ++でのこの実装はどこにありますか?このさまざまな実装の背後にある理由は何ですか? – King

+0

基本的に、iostreamまたはベクトルヘッダーにはbasic_stringを含むファイルが含まれますが、は含まれません。私はどちらが分かりません。それは、iostreamがbasic_stringsか何かを含む例外を含むようなものかもしれません。 –

0

これはMSVS 2005で私にとってはうまくいきます。これは新しいものではありません。

あなたのコンパイラが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

関連する問題