I want to overload the operator< for std::pair class.
template <>
bool operator< (const std::pair<int, std::string>& lhs, const std::pair<int, std::string>& rhs) {
return lhs.first > rhs.first || (!(rhs.first > lhs.first) && lhs.second < rhs.second);
};
I get the following error: no function template matches function template specialization 'operator<'
I also tried:
template <>
bool std::pair<int, std::string>::operator< (const std::pair<int, std::string>& lhs, const std::pair<int, std::string>& rhs) {
return lhs.first<rhs.first || (!(rhs.first<lhs.first) && lhs.second<rhs.second);
};
This time I get the error: overloaded 'operator<' must be a binary operator (has 3 parameters)
How am I supposed to do this?
template <>your 1st version should be OK. - πάντα ῥεῖtemplate <>at the beginning - Jerry Jeremiahtemplate <>specialization), that you forgot to compile your code with the-std=c++11flag. - πάντα ῥεῖx={2,"A"}andy={1, "B"}. The results of bothx<yandy<xwill betrue, which means that the overload cannot be used for ordering. - Sergey Kalinichenko