Reputation: 4344
I'd like to use stl sort with a class comparison function greater
that uses infoVec1
and infoVec2
but I'm getting a compile error:
Here is the class header
class Compare{
Compare();
std::vector< std::vector<std::string> >& infoVec1;
std::vector< std::vector<std::string> >& infoVec2;
public:
bool greater(int one, int two);
Compare(std::vector< std::vector<std::string> >& info1,
std::vector< std::vector<std::string> >& info2);
};
I've initialized Compare in main like so:
Compare C = Compare(info1, info2);
And I'm trying to use great in main like:
sort(vec.begin(), vec.end(), C.greater);
And I'm getting this error:
main.cpp:266: error: no matching function for call to ‘sort(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, <unresolved overloaded function type>)’
/usr/include/c++/4.2.1/bits/stl_algo.h:2852: note: candidates are: void std::sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, _Compare = bool (Compare::*)(int, int)]
make: *** [main.o] Error 1
How could I fix this class so that greater
will work with stl sort?
Upvotes: 0
Views: 1531
Reputation: 264411
Its easier to change the greater() method into operator()().
class Compare{
Compare();
std::vector< std::vector<std::string> >& infoVec1;
std::vector< std::vector<std::string> >& infoVec2;
public:
bool operator()(int one, int two) const; // this is used automatically.
Compare(std::vector< std::vector<std::string> >& info1,
std::vector< std::vector<std::string> >& info2);
};
Upvotes: 1