Creating a custom equality operator between structures

I want to create a custom equality operator between two structures, MyData and MyDataComparator. They are as follows:

.h file:



USTRUCT(BlueprintType)
struct FMyData
{
  GENERATED_BODY()
public:
  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "My Data")
  FString MyName;

  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "My Data")
  int32 MyInt;
};

USTRUCT(BlueprintType)
struct FMyDataComparator
{
  GENERATED_BODY()
public:
  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "My Data")
  FString MyNameToCompare;

  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "My Data")
  int32 MyIntMax;

  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "My Data")
  int32 MyIntMin;

  inline bool operator==(const FMyData& MyDataReference);
};


.cpp file:



inline bool FMyDataComparator: operator==(const FMyData & MyDataReference)
{
  return MyNameToCompare.Equals(MyDataReference.MyName) && MyIntMax > MyDataReference.MyInt && MyIntMin < MyDataReference.MyInt;
}


The actual .cpp file compiles, its the .cpp.obj file which complains:



Manager.cpp.obj : error LNK2019: unresolved external symbol "public: bool __cdecl FMyDataComparator::operator==(struct FMyData const &)" (...)


I’m assuming this is more an Unreal 4 thing than a C++ thing… why does the cpp.obj file have an unresolved external symbol?

1 Like

Answer (tsk):

In the header, this:



inline bool operator==(const FMyData& MyDataReference);


Needs to be replaced with this:



FORCEINLINE bool operator==(const FMyData& MyDataReference)
{
  return MyNameToCompare.Equals(MyDataReference.MyName) && MyIntMax > MyDataReference.MyInt && MyIntMin < MyDataReference.MyInt;
}


and the cpp definition needs to be removed. Hope this helps someone in the future…

2 Likes

This is due to a misunderstanding of what ‘inline’ keyword is.

cppreference.com

Conclusion: trust the compiler.

This doesn’t work at all unfortunately. Certainly not in a blueprint context.

This is a C++ operator, and as you can see by the lack of macros around it, it is now know to the reflection system. There’s nothing surprising here. Custom operators obviously do work very well in a C++ context.

If you need to use them in a BP context, I would suggest exposing a UFUNCTION which c++'s code uses your equality operator. Since I don’t believe UFUNCTIONs are supported in structs, I would suggest declaring a C++ comparison function in a UBlueprintFunctionLibrary.