2016-11-05 3 views
2

私は2つのstd :: array同じサイズを持っていて、同じ要素の型(私が書いたクラス)を格納するのは==演算子コンパイラこのエラーが発生します:...\include\xutility(2919): error C2672: 'operator __surrogate_func': no matching overloaded function foundstd :: array私のクラスを要素として持つ2つの配列を比較できません

私はベクトルを要素としてトウ配列を比較しようとしましたが、それは機能しましたが、どのクラスでも配列を比較するとエラーが発生します。

Testクラス:

class Class { 

    int i; 

public: 
    Class() {} 
    Class(const Class& other) {} 
    Class(Class&& other) {} 
    ~Class() {} 

    Class operator= (const Class& other) {} 
    Class operator= (Class&& other) {} 

    BOOL operator== (const Class& other) {} 

}; 

比較:

std::array<Class, 3> a0 = {}; 
    std::array<Class, 3> a1 = {}; 

     if (a0 == a1) 
     "COOL"; 

エラー私は取得しています:

...\include\xutility(2919): error C2672: 'operator __surrogate_func': no matching overloaded function found 
+0

なぜあなたは '' BOOL'を返す代わりにされていますなぜあなたの関数にreturn文がありませんか? – krzaq

+0

あなたは 'a0'と' a1'を比較するのではなく、配列を比較しているからです。 –

+0

@ El Sadoff a1とa0は配列Lol –

答えて

6

あなたがoperator==std::arrayの定義を見ると、あなたがよそれがconst配列に対して定義されていることに注意してください。これは、あなたのClassoperator==が持たない要素としてconstにしかアクセスできないことを意味します。 、あなたはおそらくbool代わりのBOOLを返すようにしたい、それでいる間

BOOL operator== (const Class& other) const { /*...*/ } 
            ^^^^^ 

:それはのconstとしてこの暗黙のを取るために

変更

bool operator== (const Class& other) const { /*...*/ } 
+1

このリンクはあなたの答えの1つを指していますが、これは 'operator =='の実際の定義ではなく、質問とは関係ありません。これは意図されていますか? – Rakete1111

+0

@ Rakete1111いいえ、コピーペーストエラーである必要があります。編集されました。ありがとう! – krzaq

関連する問題