C++ - 按字母顺序排列的字符串 - '<'操作员过载

发布于 2024-10-06 19:32:09 字数 1999 浏览 4 评论 0 原文

对于初学者来说,这是家庭作业,我很清楚我应该做什么,但我显然错过了一些东西。

我目前有一个名为“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;

};

For starters this is homework, I have a good understanding of what i'm supposed to do but I am obviously missing something.

I currently have an abstract base class called "Person". And I have 3 classes that inherit person they are Staff, Faculty, and Student.

I am trying to organize all the "Person's" names by last name. So I must overload the '<' operator.

I have the function written but I just don't know where to put it.

Function:

bool operator < ( const Faculty &right )
        {
            if( getLastName() >= right.getLastName() == 0 )
                return true;
            return false;
        }

Should I put this in the header file for all my derived classes, or should I put it as a virtual function in the base class Person? Or should I do Both. Currently I am doing both and I'm getting an error for every file.

error:

error C2662: 'Person::getLastName' : cannot convert 'this' pointer from 

Update:
I have changed my function to:

    bool operator < ( const Person &right )
    {
        return LastName >= right.getLastName(); 
    }

After getting advice from other people, I have only placed this function in "Person" and made it not virtual. Yet I am still getting 5 of the same exact errors that all point to this function.

Error:

'Person::getLastName' : cannot convert 'this' pointer from 'const Person' to 'Person &'

If it helps anyone at all here is the code to my "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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

盛夏尉蓝 2024-10-13 19:32:10

首先,你希望这对所有人都有效,所以你应该把它放在个人身上。如果你想比较任意两个人,那么 RHS 应该是 person。

另外,你的逻辑是双重否定。我不知道你为什么要这么做,当……

bool operator < ( const Person &right )
        {
            return getLastName() < right.getLastName();
        }

更有意义时。

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...

bool operator < ( const Person &right )
        {
            return getLastName() < right.getLastName();
        }

... makes a lot more sense.

九局 2024-10-13 19:32:10

当您尝试对 const 对象调用运算符时,可能会出现此错误。编译器不知道 operator< 不会更改调用它的对象,因此会给出错误。为了确保函数不会更改对象,请将函数声明为 const:

bool operator < ( const Faculty &right ) 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 that operator< 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 as const:

bool operator < ( const Faculty &right ) const {
   ...
}

This way the function can also be called on constant objects. getLastName() probably also should be const.

暗喜 2024-10-13 19:32:10

看起来您可能需要添加或更改 getter 从:

    string getFirstName();
    string getLastName();
    string getMiddleName();
    string getSSN();
    string getFullName();

    string getFirstName() const;
    string getLastName() const;
    string getMiddleName() const;
    string getSSN() const;
    string getFullName() const;

这是因为给您错误的函数没有 Person 实例的可变版本,但是没有 const getters,所以它根本不能使用任何吸气剂!

it looks like you might need to add or change your getters from:

    string getFirstName();
    string getLastName();
    string getMiddleName();
    string getSSN();
    string getFullName();

into

    string getFirstName() const;
    string getLastName() const;
    string getMiddleName() const;
    string getSSN() const;
    string getFullName() const;

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!

混吃等死 2024-10-13 19:32:10

您应该将它放在您的 Person 类中,如果您无法想象派生类需要更改顺序,则它不需要是虚拟的。鉴于名称的排序对于不同的 Person 类别似乎没有什么不同,因此未指示 virtual

参数应该是 const Person& ,并且函数本身应该是 const (将其放在引入实现的 { 之前,或者- 如果实现不符合,在尾随 ;

之前:我

在下面添加了一个实现:

  • operator< 是一个成员函数,所以可以访问私有成员变量,而无需通过公共成员函数(例如getLastName()),在某种意义上使用公共成员函数更好(由于实现更改而需要重写的可能性较小),但我在下面偷懒了,使用了更短的直接访问
  • 来确保我们在 LastName 相等时对其他字段进行比较,依此类推。假定是唯一的,以确保即使是两个同名的人也会有可预测、可重复的排序,如果您想为 Person 对象提供“稳定”的排序顺序,例如使用时,这是至关重要的。这些对象位于 std::map 中。将 operator< 编写得像这样稳定是一个很好的经验法则,尽管它往往会更冗长并且有时执行速度会更慢。

实现:

bool operator<(const Person& right) const
{ 
    return LastName < right.LastName ? true :
           LastName > right.LastName ? false :
           FirstName < right.FirstName ? true :
           Firstname > right.FirstName ? false :
           MiddleName < right.MiddleName ? true :
           MiddleName > right.MiddleName ? false :
           SSN < right.SSN; // assume SSN is guaranteed unique
}

...另一种流行的编写方式是...

bool operator<(const Person& right) const
{ 
    return LastName < right.LastName ||
           LastName == right.LastName &&
               (FirstName < right.FirstName ||
                Firstname == right.FirstName &&
                    (MiddleName < right.MiddleName ||
                     MiddleName == right.MiddleName &&
                         SSN < right.SSN)); // assume SSN is guaranteed unique
}

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 made const (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.
  • The cascading comparisons to ensure we compare on other fields when LastNames 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 for Person objects, e.g. necessary to use these objects in a std::map<Person, XXX>. It's a good rule of thumb to write operator< to be stable like this, although it tends to be a bit more verbose and can sometimes be slower to execute.

Implementation:

bool operator<(const Person& right) const
{ 
    return LastName < right.LastName ? true :
           LastName > right.LastName ? false :
           FirstName < right.FirstName ? true :
           Firstname > right.FirstName ? false :
           MiddleName < right.MiddleName ? true :
           MiddleName > right.MiddleName ? false :
           SSN < right.SSN; // assume SSN is guaranteed unique
}

...another popular way of writing this is...

bool operator<(const Person& right) const
{ 
    return LastName < right.LastName ||
           LastName == right.LastName &&
               (FirstName < right.FirstName ||
                Firstname == right.FirstName &&
                    (MiddleName < right.MiddleName ||
                     MiddleName == right.MiddleName &&
                         SSN < right.SSN)); // assume SSN is guaranteed unique
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文