I have two structs that should compare equal memberwise. I figured the c++ compiler would generate a comparison function by itself, but it doesn’t seem to know how. Here’s the code:
#include <iostream>
struct MyEntity
{
int a;
int b;
};
int main()
{
const MyEntity e1{1,2};
MyEntity e2{1,2};
if (e1 == e2)
{
std::cout << "Hello World" << std::endl;
}
}
The error will be:
<source>: In function 'int main()':
<source>:13:12: error: no match for 'operator==' (operand types are 'const MyEntity' and 'MyEntity')
13 | if (e1 == e2)
| ~~ ^~ ~~
| | |
| | MyEntity
| const MyEntity
Is there a way to nudge the compiler into generating an operator== (const MyEntity& other)
const or do I have to code it myself?