I implemented a custom TimeStruct class, where I declare < operator as follows
bool operator<(const TimeStruct t2) const;
implementation is as follows.
bool TimeStruct::operator<(const TimeStruct t2) const
{
//do something, I don't include the actual implementation
return true;
}
Then I have another class X where this TimeStruct class is a member, let’s call this member field Y. And I am using a vector, and I want to sort this vector on Y field of Class X. Therefore I will need to specify a method which might serve as a basis for the vector’s sort method.
Therefore I declared an additional method in X to compare different X’s.
bool compareX(const X& x1, const X& x2) const;
and the implementation is as follows:
bool X::compareX(const X& x1, const X& x2) const
{
return (x1.Y.operator<(x2.Y));
}
Unfortunately this code does not compile. I am receiving the following error.
No matching function call for TimeStruct::operator<(const TimeStruct&) const
candidates are : bool TimeStruct::operator<(TimeStruct&) const
I’ve been scratching my hair for the last hour, can anybody point out what I’ve been doing wrong.
You need to pass by const reference in your
TimeStructoperator:and, according to the error message, you have provided