4

我正在尝试在结构中重载这些运算符: <, <=, ==, >=, >, 可能稍后再重载!=

将结构的一个对象与同一结构的另一个对象进行比较似乎很容易,因为在为该场景重载运算符时,定义是自动对称的。

但是,如果我想将我的 structFOOD与 a进行比较int怎么办?这也很容易,只要FOOD先出现,但是,int 先出现的场景呢?如果没有 g++ 给我这么多“必须只包含一个参数”错误,我该如何定义呢?

我意识到

bool operator> (const int &, const FOOD &) const;

由于“不止一个论点”的事情而出现问题。我明白了。

在我在互联网上搜索的所有论坛上,每个人的解决方案似乎都在使用friend,但他们的困难总是在类的上下文中,而不是结构的上下文中。这是如何为 s 结构完成的?

struct FOOD {
    int foodID;
    double price;
    bool operator> (const FOOD  &) const;       //FOOD >  FOOD
    bool operator>=(const FOOD  &) const;       //FOOD >= FOOD
    bool operator==(const FOOD  &) const;       //FOOD == FOOD
    bool operator<=(const FOOD  &) const;       //FOOD <= FOOD
    bool operator< (const FOOD  &) const;       //FOOD <  FOOD
    bool operator> (const int   &) const;       //FOOD >  int 
    bool operator>=(const int   &) const;       //FOOD >= int
    bool operator==(const int   &) const;       //FOOD == int
    bool operator<=(const int   &) const;       //FOOD <= int
    bool operator< (const int   &) const;       //FOOD <  int
    bool operator> (const int &, const FOOD &) const;   // int >  FOOD
    bool operator>=(const int &, const FOOD &) const;   // int >= FOOD
    bool operator==(const int &, const FOOD &) const;   // int == FOOD
    bool operator<=(const int &, const FOOD &) const;   // int <= FOOD
    bool operator< (const int &, const FOOD &) const;   // int <  FOOD
};

bool FOOD::operator> (const FOOD  &f) const {return foodID >  f.foodID;}//FOOD >  FOOD
bool FOOD::operator>=(const FOOD  &f) const {return foodID >= f.foodID;}//FOOD >= FOOD
bool FOOD::operator==(const FOOD  &f) const {return foodID == f.foodID;}//FOOD == FOOD
bool FOOD::operator<=(const FOOD  &f) const {return foodID <= f.foodID;}//FOOD <= FOOD
bool FOOD::operator< (const FOOD  &f) const {return foodID <  f.foodID;}//FOOD <  FOOD
bool FOOD::operator> (const int   &i) const {return foodID >  i;}   //FOOD >  int 
bool FOOD::operator>=(const int   &i) const {return foodID >= i;}   //FOOD >= int
bool FOOD::operator==(const int   &i) const {return foodID == i;}   //FOOD == int
bool FOOD::operator<=(const int   &i) const {return foodID <= i;}   //FOOD <= int
bool FOOD::operator< (const int   &i) const {return foodID <  i;}   //FOOD <  int
bool FOOD::operator> (const int &i, const FOOD &f) const {return i >  f.foodID;}// int >  FOOD
bool FOOD::operator>=(const int &i, const FOOD &f) const {return i >= f.foodID;}// int >= FOOD
bool FOOD::operator==(const int &i, const FOOD &f) const {return i == f.foodID;}// int == FOOD
bool FOOD::operator<=(const int &i, const FOOD &f) const {return i <= f.foodID;}// int <= FOOD
bool FOOD::operator< (const int &i, const FOOD &f) const {return i <  f.foodID;}// int <  FOOD

g++给了我这些错误:

structsTransAndFood.cc:64:45: error: ‘bool FOOD::operator>(const int&, const FOOD&) const’ must take exactly one argument
4

2 回答 2

6

在结构之外定义它FOOD

bool operator> (const int &i, const FOOD &f) {return i >  f.foodID;}

不要忘记从FOOD结构中删除这个错误的声明:

bool operator> (const int &, const FOOD &) const; // <--- remove it

以及相应的定义:

bool FOOD::operator> (const int &i, const FOOD &f) const {return i >  f.foodID;} // <--- remove it

对其他运算符重复相同的操作。

解决方案

你声称你理解“不止一个论点”。然而,事实证明你没有。我不明白为什么您很难理解编译器和这里的 2 个人告诉您的内容,但解决方案如下所示:

struct FOOD {
    int foodID;
    double price;

    bool operator> (const FOOD  &) const;
    bool operator>=(const FOOD  &) const;
    bool operator==(const FOOD  &) const;
    bool operator<=(const FOOD  &) const;
    bool operator< (const FOOD  &) const;

    bool operator> (const int   &) const;
    bool operator>=(const int   &) const;
    bool operator==(const int   &) const;
    bool operator<=(const int   &) const;
    bool operator< (const int   &) const;
};

bool FOOD::operator> (const FOOD  &f) const {return foodID >  f.foodID;}
bool FOOD::operator>=(const FOOD  &f) const {return foodID >= f.foodID;}
bool FOOD::operator==(const FOOD  &f) const {return foodID == f.foodID;}
bool FOOD::operator<=(const FOOD  &f) const {return foodID <= f.foodID;}
bool FOOD::operator< (const FOOD  &f) const {return foodID <  f.foodID;}

bool FOOD::operator> (const int   &i) const {return foodID >  i;}
bool FOOD::operator>=(const int   &i) const {return foodID >= i;}
bool FOOD::operator==(const int   &i) const {return foodID == i;}
bool FOOD::operator<=(const int   &i) const {return foodID <= i;}
bool FOOD::operator< (const int   &i) const {return foodID <  i;}

bool operator> (const int &i, const FOOD &f) {return i >  f.foodID;}
bool operator>=(const int &i, const FOOD &f) {return i >= f.foodID;}
bool operator==(const int &i, const FOOD &f) {return i == f.foodID;}
bool operator<=(const int &i, const FOOD &f) {return i <= f.foodID;}
bool operator< (const int &i, const FOOD &f) {return i <  f.foodID;}
于 2013-04-18T21:44:17.177 回答
2

这类运营商

bool operator> (const int &, const FOOD &) const; 

不能成为会员。成员函数需要一个额外的隐式类型参数FOOD*(可能是 cv 限定的),因此上面的示例实际上需要三个参数。您需要将其设为非会员。

我认为最简单的方法是给你的类一个显式的int转换运算符

explicit operator int() const { return foodID; }

另请注意,所有比较运算符都可以用单个比较运算符表示,例如bool operator<(此技术用于 std 库关联容器中)

于 2013-04-18T21:38:39.577 回答