C++ - 按字母顺序排列的字符串 - '<'操作员过载
对于初学者来说,这是家庭作业,我很清楚我应该做什么,但我显然错过了一些东西。
我目前有一个名为“Person”的抽象基类。我有 3 个继承人的类,它们是员工、教师和学生。
我正在尝试按姓氏组织所有“人”的名字。所以我必须重载 '<'操作员。
我已经写了这个函数,但我只是不知道把它放在哪里。
函数:
bool operator < ( const Faculty &right )
{
if( getLastName() >= right.getLastName() == 0 )
return true;
return false;
}
我应该将其放在所有派生类的头文件中,还是应该将其作为虚函数放在基类 Person 中?或者我应该两者都做。目前我正在执行这两项操作,但每个文件都出现错误。
错误:
error C2662: 'Person::getLastName' : cannot convert 'this' pointer from
更新: 我已经将我的功能更改为:
bool operator < ( const Person &right )
{
return LastName >= right.getLastName();
}
在得到其他人的建议后,我只将此功能放在“Person”中,并且使其不是虚拟的。然而我仍然收到 5 个完全相同的错误,它们都指向这个函数。
错误:
'Person::getLastName' : cannot convert 'this' pointer from 'const Person' to 'Person &'
如果它对任何人有帮助的话,这里是我的“Person.h”的代码:
class Person
{
private:
string FirstName,
LastName,
MiddleName,
SSN;
string FullName;
public:
Person();
Person(string, string, string, string);
Person(string);
string getFirstName();
string getLastName();
string getMiddleName();
string getSSN();
string getFullName();
void setFirstName(string);
void setLastName(string);
void setMiddleName(string);
void setSSN(string);
void setFullName(string);
virtual string getIdentity()
{
return FirstName + " " + MiddleName + " " + LastName + " " + SSN;
}
bool operator < ( const Person &right )
{
return LastName >= right.getLastName();
}
virtual string getPurpose() = 0;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,你希望这对所有人都有效,所以你应该把它放在个人身上。如果你想比较任意两个人,那么 RHS 应该是 person。
另外,你的逻辑是双重否定。我不知道你为什么要这么做,当……
更有意义时。
Firstly, you want this to work on all people, so you should put it in Person. And you want to compare any two people, so the RHS should be person.
Also, your logic is a double negative. I'm not sure why you would do that, when...
... makes a lot more sense.
当您尝试对
const
对象调用运算符时,可能会出现此错误。编译器不知道operator<
不会更改调用它的对象,因此会给出错误。为了确保函数不会更改对象,请将函数声明为 const:这样也可以在常量对象上调用该函数。
getLastName()
可能也应该是const
。The error you get probably occurs when you try to call the operator on a
const
object. The compiler doesn't know thatoperator<
doesn't change the object it is called on and therefore gives an error. To ensure that the function doesn't change the object, declare the function asconst
:This way the function can also be called on constant objects.
getLastName()
probably also should beconst
.看起来您可能需要添加或更改 getter 从:
到
这是因为给您错误的函数没有 Person 实例的可变版本,但是没有
const
getters,所以它根本不能使用任何吸气剂!it looks like you might need to add or change your getters from:
into
This is because the function giving you errors doesn't have a mutable version of the Person instance, But there are no
const
getters, so it can't use any getters at all!您应该将它放在您的 Person 类中,如果您无法想象派生类需要更改顺序,则它不需要是虚拟的。鉴于名称的排序对于不同的 Person 类别似乎没有什么不同,因此未指示
virtual
。参数应该是 const Person& ,并且函数本身应该是 const (将其放在引入实现的
{
之前,或者- 如果实现不符合,在尾随;
之前:我
在下面添加了一个实现:
operator<
是一个成员函数,所以可以访问私有成员变量,而无需通过公共成员函数(例如getLastName()
),在某种意义上使用公共成员函数更好(由于实现更改而需要重写的可能性较小),但我在下面偷懒了,使用了更短的直接访问LastName
相等时对其他字段进行比较,依此类推。假定是唯一的,以确保即使是两个同名的人也会有可预测、可重复的排序,如果您想为Person
对象提供“稳定”的排序顺序,例如使用时,这是至关重要的。这些对象位于std::map
中。将operator<
编写得像这样稳定是一个很好的经验法则,尽管它往往会更冗长并且有时执行速度会更慢。实现:
...另一种流行的编写方式是...
You should put it in your Person class, and it doesn't need to be virtual if you can't imagine a derived class needing to change the ordering. Given sorting of names doesn't seem to be something that would differ for the different categorisations of Person,
virtual
isn't indicated.The argument should be a
const Person&
, and the function itself should be madeconst
(put that just before the{
introducing the implementation, or - if the implementation is out of line, before the trailing;
.EDIT: I've added an implementation below.
Things to note:
operator<
is a member function, so can access private member variables without needing to go through public member functions (e.g.getLastName()
). Using public member functions is nicer in one sense (less chance of needing to be rewritten due to implementation changes), but I've been lazy below and used the shorter direct access.LastName
s are equal, and so on. This concludes with comparing the SSN, which I assume is unique, to ensure that even two people with the same name will have a predictable, repeatable ordering. That's essential if you want to have a "stable" sort order forPerson
objects, e.g. necessary to use these objects in astd::map<Person, XXX>
. It's a good rule of thumb to writeoperator<
to be stable like this, although it tends to be a bit more verbose and can sometimes be slower to execute.Implementation:
...another popular way of writing this is...