Dear StackOverflow community,
Having the type Couple
defined like this:
class Couple
{
public:
Couple(Thing* aa, Thing* bb) : a(aa), b(bb) {};
public:
bool operator == (const Couple& rhs) const { return this->a->unique_id == rhs.a->unique_id && this->b->unique_id == rhs.b->unique_id; }
bool operator != (const Couple& rhs) const { return this->a->unique_id != rhs.a->unique_id || this->b->unique_id != rhs.b->unique_id; }
bool operator <= (const Couple& rhs) const { return this->a->unique_id < rhs.a->unique_id || this->a->unique_id == rhs.a->unique_id && this->b->unique_id <= rhs.b->unique_id; }
bool operator >= (const Couple& rhs) const { return this->a->unique_id > rhs.a->unique_id || this->a->unique_id == rhs.a->unique_id && this->b->unique_id >= rhs.b->unique_id; }
bool operator < (const Couple& rhs) const { return this->a->unique_id < rhs.a->unique_id || this->a->unique_id == rhs.a->unique_id && this->b->unique_id < rhs.b->unique_id; }
bool operator > (const Couple& rhs) const { return this->a->unique_id > rhs.a->unique_id || this->a->unique_id == rhs.a->unique_id && this->b->unique_id > rhs.b->unique_id; }
operator Couple() const { return *this; }
public:
Thing* a;
Thing* b;
};
I'm trying to use c++ std::find
function:
std::vector<Couple*> generate_couples(std::vector<Thing*> things)
{
std::vector<Couple*> couples;
for (unsigned int i = 0; i < things.size(); i++)
{
for (unsigned int j = 0; j < things.size(); j++)
{
if (things[i] != things[j] && match(things[i], things[j]))
{
Couple c = Couple(things[i], things[j]);
Couple rc = Couple(things[j], things[i]);
auto it_c = std::find(couples.begin(), couples.end(), c);
auto it_rc = std::find(couples.begin(), couples.end(), rc);
if (it_c == couples.end() && it_rc == couples.end())
couples.push_back(&c);
}
}
}
return couples;
}
Visual Studio Community 2017 gives an Error:
Error C2679 binary '==': no operator found which takes a right-hand operand of type 'const _Ty' (or there is no acceptable conversion)
originated from line 3520 of the file
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xutility
I've overloaded all the comparison operators, including '==', I've been trying to overload conevrsion operator as it's noted in the last part of an error. I've also tried to define operator ==
outside the class and declaring it as a friend function:
bool operator == (const Couple& lhs, const Couple& rhs)
{ return lhs.a->unique_id == rhs.a->unique_id && lhs.b->unique_id == rhs.b->unique_id; }
class Couple
{
// ...
friend bool operator == (const Couple& lhs, const Couple& rhs);
// ...
};
and it raises an error:
E0344 too many parameters for this operator function
There are several related questions, the most similar one is this (How to use std::find/std::find_if with a vector of custom class objects?), but I couldn't find solution to my problem there.
Thanks in advance for any comments and answers!