【发布时间】:2009-10-12 01:52:07
【问题描述】:
我有一个 Student 对象向量,我想使用 #include <algorithm> 和 sort(list.begin(), list.end()); 对其进行排序
为了做到这一点,我知道我需要重载“
这是我最近的尝试:
在 Student.h...
...
using namespace std;
class Student
{
friend bool operator <(const Student& first, const Student& second);
public:
...
private:
...
};
在 Student.cpp...
...
#include "Student.h"
using namespace std;
...
bool operator <(const Student& first, const Student& second)
{
return first.Name() < second.Name();
}
其中“Name()”是一个返回字符串的常量函数。
程序编译并运行,但在排序过程中从未调用过我的运算符函数,当我尝试比较两个学生对象(如 s1 < s2)时,我收到“错误:未找到重载运算符”
如何正确重载此运算符,以便我的排序按预期工作?
【问题讨论】: