如何解决错误:将自定义结构作为键的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);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。