对对象指针的矢量进行排序

Sort vector of object pointers

本文关键字:排序 对象 指针      更新时间:2023-10-16

我正在创建一个种族的c++程序。我有种族课。每一场比赛都有一个名称、距离和结果指针向量(这是每个参与者的结果)。result类有一个指向参与者的指针和一个时间。上课时间有小时、分钟和秒。我想对结果的矢量从最快的时间到最慢的时间进行排序,所以为了比较结果,我在类result中创建了函数bool operator <(Result& res2) const

.h文件中的所有函数都实现了,我只是不想全部展示它们。

我几乎可以肯定函数sortResults是不对的,但函数operator<给了我不知道如何解决的错误。它在所有if语句中都给了我这个错误:这行有多个标记

- passing 'const Time' as 'this' argument of 'unsigned int Time::getHours()' discards qualifiers [-
 fpermissive]
- Line breakpoint: race.cpp [line: 217]
- Invalid arguments ' Candidates are: unsigned int getHours() '

你能告诉我我做错了什么吗?

.h文件:

class Time
{
    unsigned int hours;
    unsigned int minutes;
    unsigned int seconds;
public:
    Time(unsigned int h, unsigned int m, unsigned int s, unsigned int ms);
    Time();
    unsigned int gethours();
    unsigned int getMinuts();
    unsigned int getSeconds();
    string show();
};
class Participant {
    string name;
    unsigned int age;
    string country;
public:
    Participant(string n, unsigned int a, string c);
    string getName();
    string getCountry();
    int getAge();
    string show() const;
};
class Result {
    Participant *part;
    Time time;
public:
    Result(Participant  *p, Time t);
    Participant *getParticipant() const;
    Time getTime();
    string show();
    bool operator <(Result& res2) const;
};
class Race {
    string name;
    float distance;
    vector<Result *> results;
public:
    Race(string nm, float dist);
    string getName();
    void setName(string nm);
    float getDistance();
    vector<Result *> sortResults();
    void addResult(Result *r);
    string showRaceResults();
    string show();
};

.cpp文件:

bool Result::operator <(Result& res2) const {
    if (time.gethours() < res2.getTime().gethours())
        return true;
    else {
        if (time.gethours() > res2.getTime().gethours())
            return false;
        else {
            if (time.getMinutes() < res2.getTime().getMinutes())
                return true;
            else {
                if (time.getMinutes() > res2.getTime().getMinutes())
                    return false;
                else {
                    if (time.getSeconds() < res2.getTime().getSeconds())
                        return true;
                    else {
                            return false;
                        }
                }
            }
        }
    }
}
vector<Result *> Race::sortResults() {
    sort (results.begin(), results.end(), operator <);
return results;
}

您应该声明并定义Time::gethours()为常量成员函数。

class Time {
// ...
    unsigned int gethours() const;  // note the "const"
};
unsigned int Time::gethours() const { /* ... */ } // note the "const"

要对vector<Result*>进行排序,您需要

  1. Result::operator<()更改为以const Result&而不是Result&作为参数
  2. Result*定义一个类似static bool cmpResultPtr(const Result* lhs, const Result* rhs) { return *lhs < *rhs; }的比较函数
  3. 致电sort(results.begin(), results.end(), cmpResultPtr);

函数cmpResultPtr()可以在.cpp文件中定义,先于Race::sortResults()的定义。

您声明运算符<为const,但它调用的Time::getHours没有相同的声明。对于编译器来说,这意味着getHours可以更改时间成员的值,这违反了运算符<的常量;。