 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Michael T. Peterson Guest
|
Posted: Sun Aug 29, 2004 11:42 pm Post subject: [Q] HOWTO overload '==' operator? |
|
|
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
|
|
| Back to top |
|
 |
David Hilsee Guest
|
Posted: Sun Aug 29, 2004 11:50 pm Post subject: Re: [Q] HOWTO overload '==' operator? |
|
|
"Michael T. Peterson" <mtp1032 (AT) comcast (DOT) net> wrote
| Quote: | 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
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Mon Aug 30, 2004 1:00 am Post subject: Re: [Q] HOWTO overload '==' operator? |
|
|
"Michael T. Peterson" <mtp1032 (AT) comcast (DOT) net> wrote...
| Quote: | 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
|
|
| Back to top |
|
 |
Thomas Matthews Guest
|
Posted: Mon Aug 30, 2004 2:13 pm Post subject: Re: [Q] HOWTO overload '==' operator? |
|
|
Michael T. Peterson wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
Michael T. Peterson Guest
|
Posted: Mon Aug 30, 2004 2:41 pm Post subject: Re: [Q] HOWTO overload '==' operator? |
|
|
Thanks, everyone. all of your insights haven proven very helpful.
Cheers,
Michael
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|