Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error C2678: binary '<'

Status
Not open for further replies.

COTLG

Technical User
Apr 15, 2003
107
0
0
US
Hi All,

I am carrying out operator overload for a class - ErrorType and I am getting this error on compiling ErrorType.cpp:

errortype.cpp(43) : error C2678: binary '<' : no operator found which
takes a left-hand operand of type 'const ErrorType' (or there is no
acceptable conversion)

Here is the portion of code:

bool ErrorType::eek:perator<(const ErrorType& right)
{
if((priority < right.priority)||
(priority == right.priority &&
timestamp < right.timestamp)||
(priority == right.priority &&
timestamp == right.timestamp &&
errorCode < right.errorCode))
return true;
else
return false;
}


bool ErrorType::eek:perator>(const ErrorType& right)
{
return right < *this; //line 43
}

I will appreciate any assistance.

Chike.
 
My guess is your operator<() isn't defined as a const function.
If possible, it would be better to define the operator<() function(s) as non-member, non-friend functions.
Example:
Code:
class ErrorType
{
    ...
}

bool operator<( const ErrorType& lhs, const ErrorType& rhs )
{
    // Put your code here by calling only public functions.
}
 
Thanks cpjust,

I just removed the "const" from the declarations and definitions of the operators:

bool ErrorType::eek:perator<(ErrorType& right)

I think since *this is supposed to point to an object of ErrorType and ErrorType class does not contain a "const" a conversion could not be made since the parameters passed to operator< for example, should be of the same type.

Chike.
 
A better solution than removing the const would be to add it to the appropriate places. If you want to keep the operators as members, then make them const members:
Code:
bool ErrorType::operator<(const ErrorType& right) const;
bool ErrorType::operator>(const ErrorType& right) const;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top