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