문제

I have a SetPartitionVector class which is derived from vector<SetPartition>. I'd like to partial_sort this vector using a custom comparison function but I have an error at compilation.

bool ScalableSummary::featuresDistComp(SetPartition cluster1, SetPartition cluster2){
    return (segmentClusters.AverageSOD(cluster1) > segmentClusters.AverageSOD(cluster2));
}

void ScalableSummary::selectLeastConsensualFeatures(const int p){
    partial_sort(segmentClusters.begin(), segmentClusters.begin() + p, segmentClusters.end(), featuresDistComp);
}

segmentClusters is a member of ScalableSummary of type SetPartitionVector which was filled this way :

SetPartition_ptr cluster;
...
segmentClusters.push_back(*cluster);

SetPartition_ptr is a smart pointer defined like this : typedef boost::shared_ptr<SetPartition> SetPartition_ptr;

This is the error I get from the compiler :

g++ -o ScalableSummary.o -c ScalableSummary.cpp -Iinclude -Wall -g
ScalableSummary.cpp: In member function ‘void ScalableSummary::selectLeastConsensualFeatures(int)’:
ScalableSummary.cpp:56:108: erreur: no matching function for call to ‘partial_sort(std::vector<SetPartition>::iterator, __gnu_cxx::__normal_iterator<SetPartition*, std::vector<SetPartition> >, std::vector<SetPartition>::iterator, <unresolved overloaded function type>)’
ScalableSummary.cpp:56:108: note: candidates are:
/usr/include/c++/4.6/bits/stl_algo.h:5240:5: note: template<class _RAIter> void std::partial_sort(_RAIter, _RAIter, _RAIter)
/usr/include/c++/4.6/bits/stl_algo.h:5279:5: note: void std::partial_sort(_RAIter, _RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<SetPartition*, std::vector<SetPartition> >, _Compare = bool (ScalableSummary::*)(SetPartition, SetPartition)]
/usr/include/c++/4.6/bits/stl_algo.h:5279:5: note:   no known conversion for argument 4 from ‘<unresolved overloaded function type>’ to ‘bool (ScalableSummary::*)(SetPartition, SetPartition)’
도움이 되었습니까?

해결책

The function object you pass to std::partial_sort needs to be either a callable object, or a function pointer. To make a function pointer you nedd to use the address-of operator &, just like when you make a pointer out of any other variable:

partial_sort(..., &featuresDistComp);
//                ^
//                |
// Note address-of operator here

Also, I hope your function is marked as static? You can't use non-static member functions as ordinary function pointers. The reason is that all non-static member functions have a hidden first arguments that is the this pointer inside the function. So either make sure the function is static or use e.g. std::bind:

using namespace std::placeholders;  // for _1, _2, _3...
partial_sort(..., std::bind(&ScalableSummary::featuresDistComp, this, _1, _2));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top