我遇到的问题是我想在模板类中使用 STL 的排序和自定义比较函数。
使用 typedef 的想法来自另一个Stackoverflow 帖子
无论如何,这是代码:
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]]);
}
};
我得到的错误是
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
先感谢您!