2012-05-13 4 views
0

私はこの消去機能を実行してベクターから '2'を削除しようとしたときにエラーのリストを検出しました。私はどこに問題があるのか​​分かりません。ヘルプは非常に高く評価されます!ベクトル内の特定の値を削除する

STRUCTミン

struct MyInt 
{ 
friend ostream &operator<<(ostream &printout, const MyInt &Qn) 
{ 
    printout<< Qn.value << endl; 
    return printout; 
} 

    int value; 
    MyInt (int value) : value (value) {} 
}; 

STRUCTのMyStuff

struct MyStuff 
{ 
    std::vector<MyInt> values; 

    MyStuff() : values() 
    { } 
}; 

MAIN

int main() 
{ 
MyStuff mystuff1,mystuff2; 

for (int x = 0; x < 5; ++x) 
    { 
     mystuff2.values.push_back (MyInt (x)); 
    } 

vector<MyInt>::iterator VITER; 
mystuff2.values.push_back(10); 
mystuff2.values.push_back(7); 

    //error points to the line below 
mystuff2.values.erase(std::remove(mystuff2.values.begin(), mystuff2.values.end(), 2), mystuff2.values.end()); 

    return 0; 

}

エラーメッセージ

stl_algo.h:関数内で '_OutputIteratorのstd :: remove_copy(_InputInputIterator、_InputIterator、const_Tp &)[with_InputIterator = __gnu_cxx:__ normal_iterator>>、OutputIterator = __ gnu_cxx :: __通常のイテレータ>>、Tpは= INT]'

オペレータのための一致が==」

Erorrメッセージがpartciularラインが ライン1267、1190、327、1263、208、212、216、220、228、232、実質的にstl_algo.hのラインに違反示しました。 、236

+1

「いいえ試合をオペレータのために==」何がそれについて不明ですか? 'myInt'と整数を比較できるように'演算子== '関数を定義する必要があります。 – jrok

+0

@jrok、それは答えになるはずです。それは本当に簡単です。 – chris

答えて

2

==オペレータに、MyIntクラスのオーバーロードを行う必要があります。例えば

struct MyInt 
{ 

friend ostream &operator<<(ostream &printout, const MyInt &Qn) 
{ 
    printout<< Qn.value << endl; 
    return printout; 
} 

// Overload the operator 
bool operator==(const MyInt& rhs) const 
{ 
    return this->value == rhs.value; 
} 

    int value; 
    MyInt (int value) : value (value) {} 
}; 
+0

その演算子はconstでなければなりません。 –

+0

@BenjaminLindleyそれを修正しました。それを 'const'にする目的は何ですか? –

+1

したがって、2つのconst MyIntsを比較することができます。 –

1

は、2つの問題があります。表示されているエラーは、int型と型の等しいかどうかを定義していないことを示しています。あなたの構造体では、1つの等価演算子

bool operator==(int other) const 
{ 
    return value == other; 
} 

を定義する必要がありますし、もちろん、他の方向にグローバルオペレータを定義していない:

bool operator==(int value1, const MyInt& value2) 
{ 
    return value2 == value1; 
} 
関連する問題