Custom class comparison needed for binary_search()
-
18-06-2021 - |
Question
I have a custom class and std::vector
filled with objects of this class. And I want to do binary_search
in this array.
I've overloaded operators on my class like this:
bool operator ==(const someClass&);
bool operator > (const someClass&);
bool operator < (const someClass&);
and they are working fine (they have bodies, yep).
Now I have an error Error
2 error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const someClass' (or there is no acceptable conversion)
Should I make copy constructor (already overloaded =, but it didn't helped) or add something else to operators?
Thanks.
La solution
You need to make the operator const:
bool operator < (const someClass&) const;
without that, only the RHS is const.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow