Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > [Q] HOWTO overload '==' operator?

Reply
Thread Tools

[Q] HOWTO overload '==' operator?

 
 
Michael T. Peterson
Guest
Posts: n/a
 
      08-29-2004
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


 
Reply With Quote
 
 
 
 
David Hilsee
Guest
Posts: n/a
 
      08-29-2004
"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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-30-2004
"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


 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      08-30-2004
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

 
Reply With Quote
 
Michael T. Peterson
Guest
Posts: n/a
 
      08-30-2004
Thanks, everyone. all of your insights haven proven very helpful.

Cheers,

Michael


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
howto overload with a NOP (empty statement) Stef Mientki Python 5 01-06-2007 02:34 PM
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM
UDP source ports using PAT (NAT overload) Greg Grimes Cisco 3 08-16-2004 10:26 PM
Using multiple outside interface with ip nat overload Emanuel Cisco 1 02-25-2004 08:10 AM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57