All Things C++

How to solve Error C2678 binary ‘==’: no operator found which takes operand of type ‘const T’ in UE
February 19, 2024

As always, the billboard summary is at the bottom. Now, the exact error (C2678) from your compiler:

This can occur with any custom structure and normally happens when you’ve attempted to use it as a key in a TMap or as the type for TArray. What it is essentially saying is that the compiler has no way of differentiating between two objects of that same type. Normally one can find this error when they haven’t included the ‘bool operator==’ of the error type (so operator> or operator< as well). A going-to-error-for-this-usecase structure definition would look like this:

VanillaStructDef.h

In this instance we would need to include the bool operator function and a GetTypeHash() function like so:

VanillaOperatorDefinitions.h

But what if our structure is already defined with this and we still get an error?!

'Should'BeWorkingStructDef.h

In this instance the error occurs because our definition of bool operator== is not static. For the use in TArray and TMap, the operator definition must be static.

The solution

Adding the ‘friend’ specifier to your bool operator will force the function to be static. Like so:

The compiler will automatically fill A and B when checking values of an array or map against one another to structure it correctly. So, the whole structure looks like this:

SolvedToBeWorkingStructDef.h

Copy Code

Copied!

Billboard summary

Duplicate your bool operator that is errored (whether operator==, <, or >) and add:

- the ‘friend’ specifier to make the function static

- ‘const FYourEpicStruct& VarName’ as the other to be checked against

like so:

Thanks for Reading!

See if Developd is the right fit for you.

Velit
Voluptatem Nostrum Tenetur
Voluptatum
Molestiae Et Officia
Voluptatem Nostrum Tenetur
Velit