[Q] HOWTO overload '==' operator?

  • Thread starter Michael T. Peterson
  • Start date
M

Michael T. Peterson

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
 
D

David Hilsee

Michael T. Peterson said:
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;
}
};
 
V

Victor Bazarov

Michael T. Peterson said:
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
 
T

Thomas Matthews

Michael said:
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
 
M

Michael T. Peterson

Thanks, everyone. all of your insights haven proven very helpful.

Cheers,

Michael
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top