Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading the == function

I am currently working on creating an overloaded function for the == operator. I am creating an hpp file for my linked list and I can't seem to get this operator working in the hpp file.

I currently have this:

template <typename T_>
class sq_list 
{

bool operator == ( sq_list & lhs, sq_list & rhs) 
{
    return *lhs == *rhs;
};

reference operator * ()     {
        return _c;
    };

};
}

I get about 10 errors but they pretty much repeat as errors:

C2804: binary 'operator ==' has too many parameters
C2333:'sq_list::operator ==' : error in function declaration; skipping function body
C2143: syntax error : missing ';' before '*'
C4430: missing type specifier - int assumed. Note: C++ does not support default-int

I've tried changing things around but I constanly get the same errors as above

Any tips or assistance on this is greatly appreciated.

like image 282
Johnston Avatar asked Feb 18 '12 00:02

Johnston


1 Answers

The member operator only has one argument, which is the other object. The first object is the instance itself:

template <typename T_>
class sq_list 
{
    bool operator == (sq_list & rhs) const // don't forget "const"!!
    {
        return *this == *rhs;  // doesn't actually work!
    }
};

This definition doesn't actually make sense, since it just calls itself recursively. Instead, it should be calling some member operation, like return this->impl == rhs.impl;.

like image 94
Kerrek SB Avatar answered Oct 08 '22 14:10

Kerrek SB