Hi all,
I am trying to use the function std::sort in the following way:
1 2 3 4
|
template typename<T> void SomeCLass<T>::some_memberfunction(...) {
vector<int> ind = ... // ( basically, integers from 1 to n, say)
std::sort(ind.begin(), ind.end(), comparison_func);
}
|
with
1 2 3 4
|
template typename<T> bool SomeCLass<T>::comparison_func(int a, int b)
{
return ( (this->SomeMemberVector)[a] < (this->SomeMemberVector)[b]);
}
|
I don't understand why the compiler returns, after instanciation in int of previous class:
bool (SomeClass<int>::)(int, int)’ does not match ‘bool (SomeClass<int>::*)(int, int)’ .
If anyone of you could help on that, it would be really fantastic!
Cheers everyone.
JC
EDIT: I realize that I shouldn't have posted this message in that part of the forum, sorry about that.
I think it should be posted in 'General C++ Programming' section instead. I won't double-post though.
EDIT again:
I have used another way (which does work either ...), using functors (as found while browsing the forum) :
1 2 3 4 5 6 7 8
|
template <typename T> struct CmpPairs {
CmpPairs(const vector<T> &v): v_(v) {}
Vector<T> & v;
bool operator()(int a, int b) const { return 'some comparison involving v'; };
};
template <typename T> CmpPairs<T> CreateCmpPairs(const vector<T>& v) { return CmpPairs<T>(v); };
|
then I would rewrite SomeCLass<T>::some_memberfunction as :
1 2 3 4
|
template typename<T> void SomeCLass<T>::some_memberfunction(...) {
vector<int> ind = ... // ( basically, integers from 1 to n, say)
std::sort(ind.begin(), ind.end(), CreateCmpPairs(this->SomeMemberVector));
}
|
This time, I have the following error:
|
error: there are no arguments to ‘CreateCmpPairs’ that depend on a template parameter, so a declaration of ‘CreateCmpPairs’ must be available.
|
I am so confused, because this is obviously wrong: the definition of 'CreateCmpPairs' involves 'vector<T> &v' as a template parameter! What did I do wrong ?