By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

[Q] HOWTO overload '==' operator?

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Michael T. Peterson" <mt*****@comcast.net> 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
Jul 22 '05 #2

P: n/a
"Michael T. Peterson" <mt*****@comcast.net> 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
Jul 22 '05 #3

P: n/a
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

Jul 22 '05 #4

P: n/a
Thanks, everyone. all of your insights haven proven very helpful.

Cheers,

Michael
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.