I want to sort a vector of arbitrary types, so I wrote the following code:
#include <iostream> #include <vector> #include <algorithm> using namespace std; template<class T> bool compare(T a, T b) { return a < b; } int main() { vector<int> v; v.push_back(3); v.push_back(4); v.push_back(2); v.push_back(1); sort(v.begin(), v.end(), compare); for (size_t i = 0; i < v.size(); i++) { cout << v.at(i) << " "; } return 0; } This code is not compiling, with a error message like:
..\src\Test.cpp:22:34: error: no matching function for call to 'sort(std::vector<int>::iterator, std::vector<int>::iterator, <unresolved overloaded function type>)' ..\src\Test.cpp:22:34: note: candidates are: ... and more When I implement the compare-function with concrete types, it works. Can someone tell me how to do that with a template-compare-function?