简体   繁体   English

为什么这种运算符重载方法在这里不起作用?

[英]Why does this method for operator overloading not work here?

// program in c++ to use priority_queue with class
#include <iostream>
#include <queue>
using namespace std;

#define ROW 5
#define COL 2

class Person {

public:
    int age;

    float height;

    // this is used to initialize the variables of the class
    Person(int age, float height)
        : age(age), height(height)
    {
    }

    bool operator < (const Person& p1) {
        return (this->height > p1.height);
    }
};


// bool operator<(const Person& p1, const Person& p2)
// {
//  return p1.height > p2.height;
// }

// struct myCmp
// {
//     bool operator() (const Person& p1, const Person& p2) {
//         return p1.height > p2.height;
//     }
// };


int main()
{

    priority_queue<Person> Q;

    float arr[ROW][COL] = { { 30, 5.5 }, { 25, 5 },
            { 20, 6 }, { 33, 6.1 }, { 23, 5.6 } };

    for (int i = 0; i < ROW; ++i) {

        Q.push(Person(arr[i][0], arr[i][1]));

        // insert an object in priority_queue by using
        // the Person class constructor
    }

    while (!Q.empty()) {

        Person p = Q.top();

        Q.pop();

        cout << p.age << " " << p.height << "\n";
    }
    return 0;
}

Note!笔记! The other commented methods in the code seem to work except this gives me an error.代码中的其他注释方法似乎有效,除了这给了我一个错误。

The above code is to make a priority queue with the intention that the persons with lower height are kept at the top.上面的代码是为了让身高较低的人排在最前面而创建一个优先队列。 I tried using struct method for defining the custom compare function which seems to work fine.我尝试使用 struct 方法来定义自定义比较 function 似乎工作正常。 Explicit operator overloading like in the above comments also works.上述注释中的显式运算符重载也有效。

Expected output:预期 output:

25 5 25 5

30 5.5 30 5.5

23 5.6 23 5.6

20 6 20 6

33 6.1 33 6.1

ERROR:错误:

no match for 'operator<' (operand types are 'const Person' and 'const Person') { return __x < __y; 'operator<' 不匹配(操作数类型为 'const Person' 和 'const Person'){ return __x < __y; } }

The error message:错误信息:

no match for 'operator<' (operand types are 'const Person' and 'const Person') 'operator<' 不匹配(操作数类型为 'const Person' 和 'const Person')

This tells you that the lhs and rhs object of operator are const objects.这告诉您运算符的 lhs 和 rhs object 是const对象。 and the compiler can not find an operator that will work two const Person objects.并且编译器找不到可以处理两个 const Person 对象的运算符。

If I look at your implementation:如果我查看您的实现:

bool operator < (const Person& p1) {
    return (this->height > p1.height);
}

I see that the right-hand value p1 can be a const reference.我看到右侧值p1可以是 const 引用。 But the left-hand value (the owner of the method) is being treated as non cost.但是左边的值(方法的所有者)被视为非成本。 So this implementation does not match the requirements needed.所以这个实现不符合所需的要求。

But we know this operator is not changing the state of the object so we can simply mark this as a const member function.但是我们知道这个操作符不会改变 object 的 state,所以我们可以简单地将其标记为 const 成员 function。

bool operator < (const Person& p1) const {
                             //    ^^^^^    Add the const here.
    return (this->height > p1.height);
}

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

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