Reputation: 275
I have encountered a list of errors when i was trying to execute this erase function to remove '2' from my vector. I am not sure where is the problem. Help will be greatly appreciated!
STRUCT MyInt
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;
}
Error Messages
stl_algo.h: In Function '_OutputIterator std::remove_copy(_InputInputIterator, _InputIterator, const_Tp&) [with_InputIterator = __gnu_cxx:__normal_iterator > >, OutputIterator = __ gnu_cxx::__normal iterator > >, Tp = int]'
No match for operator=='
Erorr messages showed the partciular line violated practically stl_algo.h's lines Line 1267, 1190, 327, 1263, 208, 212, 216, 220, 228, 232, 236
Upvotes: 0
Views: 205
Reputation:
You need to overload the ==
operator for class MyInt
.
For example:
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) {}
};
Upvotes: 2
Reputation: 18976
There are two problems. The error you're seeing is telling you that you haven't defined a equality comparison between int and your type. In your struct, you should define one equality operator
bool operator==(int other) const
{
return value == other;
}
and of course define a global operator in the other direction:
bool operator==(int value1, const MyInt& value2)
{
return value2 == value1;
}
Upvotes: 1