错误:将自定义结构作为键的TSortedMap,重载了运算符<

问题描述

我正在尝试使用自定义结构作为键来实现TSortedMap。我已经使结构的运算符超载了。但是,当我尝试编译时,在向TSortedMap添加元素的代码行中出现此错误

error C2678: binary '<': no operator found which takes a left-hand operand of type 'const T'
(or there is no acceptable conversion)

我的结构:

USTRUCT(BlueprintType)
struct FUtility
{
GENERATED_BODY()

public:

UPROPERTY(BlueprintReadWrite,EditAnywhere,Meta=(ClampMin = "0.0",ClampMax = "1.0"))
float value = 0.0f;

UPROPERTY(BlueprintReadWrite,Meta = (ClampMin = "0.0",ClampMax = "1.0"))
float weight = 1.0f;

FORCEINLINE bool operator== (const FUtility& other)
{
    return this->value == other.value && this->weight == other.weight;
}

FORCEINLINE bool operator< (const FUtility& other)
{
    return (this->value * this->weight) < (other.value * other.weight);
}

.....

friend uint32 GetTypeHash(const FUtility& other)
{
    return GetTypeHash(other.value) + GetTypeHash(other.weight);
}
};

不太确定为什么它不被编译,因为它已被重载。也许它没有正确地过载。任何帮助将不胜感激。

解决方法

好吧,奇怪的是我想通了。确实令我烦恼的是,文档中的所有运算符逻辑都带有两个参数。原来我只是缺少friend关键字。从那里,我可以添加第二个参数,并且可以很好地编译它。

示例:

friend bool operator< (const FUtility& a,const FUtility& b)
{
    return (a.value * a.weight) < (b.value * b.weight);
}