#### Archived

This topic is now archived and is closed to further replies.

This topic is 5614 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have templated Sorted List class that I wrote. The class works fine when I use a primitive data type(int , char, …), but if I create I list of a class that I made it gives me this error error C2678: binary ''<='' : no operator defined which takes a left-hand operand of type ''const class CCollision'' (or there is no acceptable conversion) it looks like there must be something wrong with the way I have the operators overloaded for the class. Here is the class
class CCollision {
public:

double time;

bool operator==(const CCollision& c) {
return time == c.time;
}
bool operator>(const CCollision& c) {
return time > c.time;
}
bool operator<(const CCollision& c) {
return time < c.time;
}
bool operator>=(const CCollision& c) {
return time >= c.time;
}
bool operator<=(const CCollision& c) {
return time <= c.time;
}

};

Thanks for any help.

##### Share on other sites
quote:
Original post by Abob
error C2678: binary ''<='' : no operator defined which takes a left-hand operand of type ''const class CCollision'' (or there is no acceptable conversion)
In operator dispatch, the left hand object is the one on which the operator is being called, ie lhs.operator ( rhs ). That the left hand object is const implies that you have a sorted list of const CCollisions.

Anyway, the fix is simple. A const object can only have const methods called on it - methods that do/can not change the object itself.
...bool operator == (const CCollision& c) const{  return time == c.time;} bool operator >(const CCollision& c) const{  return time > c.time;} bool operator < (const CCollision& c) const{  return time < c.time;} bool operator >= (const CCollision& c) const{ return time >= c.time;} bool operator <= (const CCollision& c) const{  return time <= c.time;}...

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 10
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632922
• Total Posts
3009223
• ### Who's Online (See full list)

There are no registered users currently online

×