The problem I’m having is that I want to use STL’s sort with a custom compare function inside a templated class.
The idea from using typedef came from another Stackoverflow Post
Anyway, here is the code:
template <typename R,typename S>
class mesh{
/* some stuff */
void sortData(){
typedef bool (*comparer_t)(const S,const S);
comparer_t cmp = &mesh::compareEdgesFromIndex;
sort(indx,indx+sides*eSize,cmp);
}
/* more stuff */
// eData and cIndx are member variables
bool compareEdgesFromIndex(const S a,const S b){
return compareEdges(eData[cIndx[2*a]],eData[cIndx[2*a+1]],eData[cIndx[2*b]],eData[cIndx[2*b+1]]);
}
};
The error I’m getting is
mesh.h:130:29: error: cannot convert ‘bool (mesh<float, unsigned int>::*)(unsigned int, unsigned int)’ to ‘comparer_t {aka\
bool (*)(unsigned int, unsigned int)}’ in initialization
Thank you in advance!
You are trying to mix a member-function-pointer where a function-pointer is required. You can either refactor the predicate to be a
staticfunction, or use bindings to associate your member-function-pointer with an instance of your classmesh.In order to bind an instance to your member-function-pointer you would do
if working with C++11. If you don’t have the luxury, then you can use equivalent functionality from Boost (replacing
std::bindbyboost::bind). C++03 offers some binding functionality but it’s limited, and I believe obsoleted now that generic binding functionality is available.