[Q] HOWTO overload '==' operator?

Discussion in 'C++' started by Michael T. Peterson, Aug 30, 2004.

  1. I am unable to figure out how to overload the '==' operator. The code
    below( cc'd it from an example on the net) doesn't compile:

    bool operator== ( const GivenName &lhs, const GivenName &rhs ) {
    return( lhs.first == rhs.first && lhs.middle == rhs.middle &&
    lhs.last == rhs.last );
    }

    The error message is 'operator ==' has too many parameters.

    Any suggestions, explanations, references to docs or books, etc., would be
    greatly appreciated.

    Cheers,

    Michael
    Michael T. Peterson, Aug 30, 2004
    #1
    1. Advertising

  2. Michael T. Peterson

    David Hilsee Guest

    "Michael T. Peterson" <> wrote in message
    news:dRtYc.82416$Fg5.21433@attbi_s53...
    > I am unable to figure out how to overload the '==' operator. The code
    > below( cc'd it from an example on the net) doesn't compile:
    >
    > bool operator== ( const GivenName &lhs, const GivenName &rhs ) {
    > return( lhs.first == rhs.first && lhs.middle == rhs.middle &&
    > lhs.last == rhs.last );
    > }
    >
    > The error message is 'operator ==' has too many parameters.
    >
    > Any suggestions, explanations, references to docs or books, etc., would be
    > greatly appreciated.


    It sounds like you made that function a member. As it is currently written,
    it should not be a member function. Example:

    class GivenName {
    // members first, middle, last, etc
    friend bool operator== ( const GivenName &lhs, const GivenName &rhs ) {
    return lhs.first == rhs.first && lhs.middle == rhs.middle &&
    lhs.last == rhs.last;
    }
    };

    --
    David Hilsee
    David Hilsee, Aug 30, 2004
    #2
    1. Advertising

  3. "Michael T. Peterson" <> wrote...
    > I am unable to figure out how to overload the '==' operator. The code
    > below( cc'd it from an example on the net) doesn't compile:
    >
    > bool operator== ( const GivenName &lhs, const GivenName &rhs ) {
    > return( lhs.first == rhs.first && lhs.middle == rhs.middle &&
    > lhs.last == rhs.last );
    > }
    >
    > The error message is 'operator ==' has too many parameters.
    >
    > Any suggestions, explanations, references to docs or books, etc., would be
    > greatly appreciated.


    Take a good look at the example from which you cc'd your code. Was the
    operator== there a member or was it a stand-alone function? Did you make
    it a member? Now, analyse the difference. Why in the example on the net
    it worked, and in your code it doesn't? What book on C++ are you reading
    at this time? Does it talk about operator overloading at all? If it does
    talk about it, have you paid attention to the difference between member/
    non-member operators, especially to the number of their arguments?

    Victor
    Victor Bazarov, Aug 30, 2004
    #3
  4. Michael T. Peterson wrote:

    > I am unable to figure out how to overload the '==' operator. The code
    > below( cc'd it from an example on the net) doesn't compile:
    >
    > bool operator== ( const GivenName &lhs, const GivenName &rhs ) {
    > return( lhs.first == rhs.first && lhs.middle == rhs.middle &&
    > lhs.last == rhs.last );
    > }
    >
    > The error message is 'operator ==' has too many parameters.
    >
    > Any suggestions, explanations, references to docs or books, etc., would be
    > greatly appreciated.
    >
    > Cheers,
    >
    > Michael
    >
    >


    As a member function of a class:
    class Bicycle
    {
    public:
    bool operator==(const Bicycle& b) const;
    };

    In your case (perhaps, as you haven't supplied much
    to work with):
    class PersonName
    {
    public:
    bool operator==(const PersonName & pn) const;
    private:
    std::string first;
    std::string middle;
    std::string last;
    };

    bool
    PersonName ::
    operator==(const PersonName& pn) const
    {
    return (last == pn.last)
    && (first == pn.first)
    && (middle == pn.middle);
    }


    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Aug 30, 2004
    #4
  5. Thanks, everyone. all of your insights haven proven very helpful.

    Cheers,

    Michael
    Michael T. Peterson, Aug 30, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr Smith
    Replies:
    7
    Views:
    453
    Mr Smith
    Feb 10, 2005
  2. Matthew Monopole

    overload operator -

    Matthew Monopole, Aug 8, 2003, in forum: C++
    Replies:
    1
    Views:
    461
    Jakob Bieling
    Aug 8, 2003
  3. Piotre Ugrumov
    Replies:
    3
    Views:
    359
    Nick Hounsome
    Jan 25, 2004
  4. Stef Mientki
    Replies:
    5
    Views:
    332
    Jussi Salmela
    Jan 6, 2007
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    228
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page