c++ - Using `std::sort` with a member function in a templated class -


the problem i'm having want use stl's sort custom compare function inside templated class.

the idea using typedef came stackoverflow post

anyway, here code:

template <typename r,typename s> class mesh{   /* 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 , cindx 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

mesh.h:130:29: error: cannot convert ‘bool (mesh<float, unsigned int>::*)(unsigned int, unsigned int)’ ‘comparer_t {aka\ bool (*)(unsigned int, unsigned int)}’ in initialization 

thank in advance!

you trying mix member-function-pointer function-pointer required. can either refactor predicate static function, or use bindings associate member-function-pointer instance of class mesh.

in order bind instance member-function-pointer

std::bind( mesh_instance, &mesh::compareedgesfromindex, _1, _2 ) 

if working c++11. if don't have luxury, can use equivalent functionality boost (replacing std::bind boost::bind). c++03 offers binding functionality it's limited, , believe obsoleted generic binding functionality available.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -