No == Operator Found While Comparing Structs in C++

No == operator found while comparing structs in C++

In C++, structs do not have a comparison operator generated by default. You need to write your own:

bool operator==(const MyStruct1& lhs, const MyStruct1& rhs)
{
return /* your comparison code goes here */
}

Can I compare 2 structures in C++?

C++ gives you attribute-by-attribute assignment implicitly, but no comparison for equality or ordering. The reason is "just because", don't look too hard into philosophy.

You must to provide those operators, if needed, by implementing them yourself explicitly, for example:

bool operator<(const Data& other) const {
if (x < other.x) return true;
if (x > other.x) return false;
return y < other.y;
}

bool operator==(const Data& other) const {
return x == other.x && y == other.y;
}

and so on.

Note also that defining for example == doesn't give you != automatically and defining < doesn't provide >= implicitly.

UPDATE

C++20 introduces (will introduce) a new operator <=> (friendly name "spaceship operator") exactly to remove the verbosity of having to define all possible relational operators. In this case adding:

std::strong_ordering operator<=>(const Data& other) const {
if (auto cmp = x <=> other.x; cmp != 0) return cmp;
return y <=> other.y;
}

will allow compilation of all relational tests (<, <=, >, >=, ==, !=) between elements of the class based on checking x first and, if that check doesn't resolve, checking y instead.

Why doesn't C provide struct comparison?

As others have mentioned, here's an extract from C: A Reference Manual by Harbison and Steele:

Structures and unions cannot be compared for equality, even though assignment for these types is allowed. The gaps in structures and unions caused by alignment restrictions could contain arbitrary values, and compensating for this would impose an unacceptable overhead on the equality comparison or on all operations that modified structure and union types.

How to use the global comparison operator when comparing two different structure types to resolve the errors in C++

As the compiler error says, you need to define operator== for your strRGBSettings struct:

struct strRGBSettings {
uint8_t red;
uint8_t green;
uint8_t blue;
bool operator==(const strRGBSettings& stg) const {
return stg.red == red && stg.blue == blue && stg.green == green;
}
};

If you cannot modify the struct, define operator== as a non-member function:

bool operator==(const strRGBSettings& stg1, const strRGBSettings& stg2) const {
return (stg1.red == stg2.red && stg1.blue == stg2.blue && stg1.green == stg2.green);
}

Note that this definition cannot go inside main, and you should at least be able to access the struct outside main.

How do you compare structs for equality in C?

C provides no language facilities to do this - you have to do it yourself and compare each structure member by member.

c++ struct as map key and operator overloading

Overload operators as const functions:

#include <iostream>
#include <map>

struct Coord_T {
uint64_t x, y;
inline bool operator==(const Coord_T& o) const { return x == o.x && y == o.y; }
inline bool operator<(const Coord_T& o) const { return x < o.x || (x == o.x && y < o.y); }
inline bool operator>(const Coord_T& o) const { return x > o.x || (x == o.x && y > o.y); }
inline bool operator!=(const Coord_T& o) const { return x != o.x || y != o.y; }
inline bool operator<=(const Coord_T& o) const { return x < o.x || (x == o.x && y <= o.y); }
inline bool operator>=(const Coord_T& o) const { return x > o.x || (x == o.x && y >= o.y); }

};

int main()
{
Coord_T coord;
coord.x = 5;
coord.y = 6;
std::map<Coord_T, bool> vals;
vals[coord] = true;
return 0;
}


Related Topics



Leave a reply



Submit