对对象指针的矢量进行排序
Sort vector of object pointers
我正在创建一个种族的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*>
进行排序,您需要
- 将
Result::operator<()
更改为以const Result&
而不是Result&
作为参数 - 为
Result*
定义一个类似static bool cmpResultPtr(const Result* lhs, const Result* rhs) { return *lhs < *rhs; }
的比较函数 - 致电
sort(results.begin(), results.end(), cmpResultPtr);
函数cmpResultPtr()
可以在.cpp文件中定义,先于Race::sortResults()
的定义。
您声明运算符<为const,但它调用的Time::getHours没有相同的声明。对于编译器来说,这意味着getHours可以更改时间成员的值,这违反了运算符<的常量;。
相关文章:
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 对象接收堆栈溢出异常 c++ 的排序向量
- 如何使用 STL 排序对具有模板专用化的自定义类对象进行排序?
- 排序谓词没有传递对索引对象的引用?
- 使用指针指向对象C++对向量进行排序
- 按类成员的顺序对包含类对象的C++向量进行排序
- 无法使用接口类型对priority_queue中的对象进行排序<T>
- 基于不同字段的对象向量的排序功能
- 基于3个对象的自定义排序
- 尝试使用比较运算符对对象向量进行排序
- C++排序的对象集
- 从文件中读取并将其内容放入对象数组中,然后对它们进行排序
- q排序类对象的列表
- std::atexit 从全局对象的构造函数调用时的排序
- 尝试对对象数组进行排序时程序终止
- std::排序为排序自定义对象时出现的向量引发错误
- 使用sort()算法C 排序对象列表
- 使用std::sort与自定义排序对象和数据类型模板-寻找正确的语法
- 快速方法部分排序对象
- 如何在动态内存中排序对象列表,在c++中使用泛型向量