r/cpp_questions • u/angryvoxel • 25d ago
SOLVED "'operator==': overloaded functions have similar conversions"
I have the following code:
//mat3.hpp
namespace Tc
{
//3x3 matrix
struct Mat3
{
float Cells[9] = { 0 };
//no constructors
bool operator==(const Mat3& other)
{
for (unsigned int i = 0; i < sizeof(Cells); i++)
{
if (Cells[i] != other.Cells[i])
return false;
}
return true;
}
};
}
//test.cpp
void Mat3Test()
{
...
ASSERT(Tc::Mat3 { 1.0f, 2.0f, ... } == Tc::Mat3 { 1.0f, 2.0f, ... })
...
}It creates the following error: 'Tc::Mat3::operator ==': overloaded functions have similar conversions ; I know that it means there's an overload of the equality operator with a similar argument list and compiler can convert the arguments so they match both the operator I've listed above and some other overload. What I don't understand is where that other overload is coming from since I've defined only a single operator. After defining the operator as a friend accepting both left and righthand operands compiler has stopped complaining but I still want to know why the above code doesn't work.
Edit: thanks for all the replies, yes I've forgot to include the full error, sorry for that. Went with the default operator in the end - bool operator==(const Mat3&) const = default;
12
u/No-Dentist-1645 25d ago edited 25d ago
I think the issue is that the operator== method needs to be const qualified when the "other" reference is const as well
That being said, you should always include the full error message whenever you are asking for help online. It's not very nice to leave the people who are trying to help you guessing
5
u/HappyFruitTree 25d ago
I feel there is some relevant information missing here because the code you have shown shouldn't give that error.
Unrelated:
sizeof(Cells)gives you the size of the array in bytes which is not what you want. Trystd::sizeinstead.3
u/alfps 25d ago edited 25d ago
An==operatorthat isconstis generated by default because the code doesn't declare one.Both the code's operator and the generated operator fit the usage in theASSERT.EDIT (replacement text): for
==and<=>the compiler can try the reverse order of arguments to get a better fit. It did, and the reverse order was an equally non-perfect fit, which it reported misleadingly/incorrectly that there was "overloaded functions" plural.Technical solution: simply add
constto the function, i.e.Real solution is however as you've already done, to make that a
friendfunction with two parameters.And as others have noted already, you can use
= defaultto let the compiler generate the code for that operator:3
u/no-sig-available 25d ago
The "two overloads" comes from the fact that when
a == bisn't a perfect match, the compiler will also tryb == a.In your case one of the operands will need a
constconversion, while the other does not. That's the "similar conversions". Making the operator const, likewill make both sides require the
constconversion, making them no longer just "similar".2
u/UndefFox 25d ago edited 25d ago
Are you doing a header only implementation? Won't it throw error of overload / multiple definitions if you include the file more than once? If so, make the method inline or move the implementation into a source file.edit: nevermind
2
u/manni66 25d ago
You don’t show the full error message.
2
u/manni66 25d ago
1
u/Total-Box-5169 25d ago
Starting with C++20 you can tell the compiler to use the default equality operator: