简体   繁体   English

C ++无法在typedef结构中重载<运算符

[英]C++ Unable to overload < operator in typedef struct

I have a typedef struct defined like so: 我有一个这样定义的typedef结构:

typedef struct myStruct {
  int id;
  double value;

  bool operator <(const myStruct &x, const myStruct &y) {
    return (x.id < y.id);
  }
} myStruct;

I need to use this struct as a key in a std::map, thus the overloaded operator. 我需要将此结构用作std :: map中的键,因此需要重载运算符。 However, I get the following error message when trying to compile this: 但是,尝试编译此消息时,出现以下错误消息:

overloaded 'operator<' must be a binary operator (has 3 parameters)

Okay, so I tried this instead: 好的,所以我尝试了以下方法:

bool operator <(const pointcloud_keyframe &x) {
  return (this->id < x.id);
}

However, that doesn't work either as I get this error message when trying to insert into the map: 但是,这也不起作用,因为当我尝试插入地图时收到此错误消息:

invalid operands to binary expression ('const myStruct' and 'const myStruct')

Please help! 请帮忙!

struct myStruct {
  int id;
  double value;

  friend bool operator <(const myStruct &x, const myStruct &y) {
    return (x.id < y.id);
  }
};

the key part is friend . 关键是friend I also removed the typedef ; 我还删除了typedef ; in C++ struct myStruct already defines a type named myStruct , no need to also typedef it. 在C ++ struct myStruct已经定义了一个名为myStruct的类型,无需对其进行typedef

There are other ways to make your code compile, but this is the easiest. 还有其他方法可以使您的代码编译,但这是最简单的。

Without friend , your operator< is a member function, and member operator< takes one argument plus an implicit this . 没有friend ,您的operator<是成员函数,成员operator<接受一个参数加一个隐式this 1 1个

With friend , it becomes a "free function" that takes 2 arguments. friend ,它成为一个带有2个参数的“自由函数”。 I find this is the cleanest way to do it. 我发现这是最干净的方法。 It still has full permission to access private bits of your struct (which it may not need). 它仍然具有访问struct私有位的完全权限(可能不需要)。

You could also move it outside of the struct itself 您也可以将其移出struct本身

struct myStruct {
  int id;
  double value;

};
inline bool operator <(const myStruct &x, const myStruct &y) {
  return (x.id < y.id);
}

but < being a friend is relatively harmless. 但是<成为friend是相对无害的。 In addition, with template types, the friend strategy scales better. 此外,使用template类型,朋友策略可更好地扩展。 So I'd get used to using it, even if technically "less permissions is better". 因此,即使技术上“权限越少越好”,我也会习惯使用它。


1 I find this annoyingly asymmetrical, so I prefer non-member < to member < . 1我发现这很不对称,因此我更喜欢非成员<而不是成员<

Given the code you've shown, you're pretty close, but not quite there ... you need to distinguish between a member function and a stand-alone "free" function. 给定已显示的代码,您已经很接近了,但还不止于此……您需要区分成员函数和独立的“免费”函数。

struct myStruct final { int id; };
inline bool operator<(const myStruct& lhs, const myStruct& rhs) {
   return lhs.id < rhs.id;
}

Keep in mind that you should prefer to use non-member functions . 请记住,您应该更喜欢使用非成员函数


Using a member function is similar, but less desirable than above: 使用成员函数是相似的,但比上面的方法更不可取:

class myStruct final
{
    int id_;
public:
    bool operator<(const myStruct& rhs) const {
       return id_ < rhs.id_;
    }
};

Another way would be to make operator<() a friend function, which has various tradeoffs; 另一种方法是使operator<()friend函数,该函数具有各种折衷; but your original code doesn't show that technique. 但是您的原始代码并未显示该技术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM