Download

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?

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…

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

cppreference.com

Conclusion: trust the compiler.