我有一个 Student 对象向量,我想使用 #include
和 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()”是一个返回字符串的常量函数。
程序编译并运行,但在排序期间以及当我尝试比较两个 Student 对象(如 s1 < s2
我收到“错误:未找到重载运算符”
如何正确重载此运算符,以便我的排序能够按我的预期工作?
I have a vector of Student objects which I want to sort using #include <algorithm>
and sort(list.begin(), list.end());
In order to do this, I understand that I need to overload the "<" operator but after trying (and failing) with several methods suggested online, I am running out of ideas.
Here is my latest attempt:
In Student.h...
...
using namespace std;
class Student
{
friend bool operator <(const Student& first, const Student& second);
public:
...
private:
...
};
And in Student.cpp...
...
#include "Student.h"
using namespace std;
...
bool operator <(const Student& first, const Student& second)
{
return first.Name() < second.Name();
}
where "Name()" is a constant function which returns a string.
The program compiles and runs, but my operator function is never called during the sort and when I tried comparing two Student objects like s1 < s2
I got an "error: overloaded operator not found"
How can I correctly overload this operator so that my sort will work as I intend?
发布评论
评论(4)
您没有说您使用的是哪个编译器,但我怀疑您使用的是一个相当新的编译器,该编译器实现了“朋友不是声明”规则。类中的friend语句不作为函数声明;包含 Student.h 的其他模块看不到该函数的任何声明。它仅在 Student.cpp 文件中可见。 (较旧的编译器没有此规则,并将友元声明视为函数声明。)
该函数不需要是友元,因为它不使用 Student 类的任何私有成员(我假设Name() 是公开的)。将函数声明移到类之外,并将“friend”替换为“extern”,它应该可以工作。
正如上面一些海报所建议的那样,可以使运算符成为成员函数,但这不是必需的。将比较运算符设置为成员函数通常不受欢迎,因为这意味着两个参数没有被对称处理(一个是不可见的“this”参数,另一个是普通的函数参数),这在某些情况下可能会导致令人惊讶的结果(例如,可以对参数应用不同的类型转换)。
You didn't say which compiler you were using, but I suspect you're using a fairly recent one that implements the "friend is not a declaration" rule. The friend statement inside the class doesn't serve as a function declaration; other modules that include Student.h don't see any declaration of the function. It's visible only inside the Student.cpp file. (Older compilers don't have this rule, and treat a friend declaration as a function declaration.)
The function doesn't need to be a friend, since it doesn't use any private members of the Student class (I'm assuming Name() is public). Move the function declaration outside the class, and replace "friend" with "extern", and it should work.
It is possible to make the operator a member function, as some posters above suggested, but it isn't necessary. Making comparison operators member functions is generally frowned upon, because it means that the two arguments aren't treated symmetrically (one is the invisible "this" argument, the other is a normal function argument), which can lead to surprising results in some cases (for example, different type conversions may be applied to the arguments).
我不会在这里使用朋友,而且我不确定它是否有效。我将使用的是...
注意尾随 const,表明在运算符< 内,*this 是常量。
编辑我无法删除这个答案,因为它已被接受,但如果可以的话我会这样做。我也无法用正确的替换它。请参阅下面的 Drew Dormanns 评论和Ross Smiths 回答。
I wouldn't use a friend here, and I'm not sure it works at all. What I would use is...
Note the trailing const, indicating that within operator<, *this is constant.
EDIT I cannot delete this answer because it was accepted, but I would if I could. I also can't replace it with a correct one. See Drew Dormanns comment below, and Ross Smiths answer.
顺便说一句,对于这么简单的函数,我个人会这样做,除非样式指南禁止在类定义中定义函数:
这种形式的“friend”声明允许您定义一个非成员 类主体内的
运算符<
。它仍然是一个友元,因此如果需要,Name()
可以是私有的。By the way, with a function this simple I would personally just do this, unless the style guide bans the definition of functions inside class definitions:
This form of the "friend" declaration lets you define a non-member
operator<
inside the body of the class. It's still a friend, soName()
can be private if desired.好吧,您可以将其作为内部运算符来完成:
通过将“this”与第二个进行比较的实现。我心中的问题是:“Name”方法是否具有 const 风格?因为如果不这样做,那么您就无法编写使用它的 const 方法。
Well, you could do it as an internal operator:
With an implementation comparing 'this' to second. The question in my mind is: Does the 'Name' method come in a const flavor? Because if it doesn't, then you can't write a const method that uses it.