我遇到的問題是我想在模板類中使用STL的自定義比較函數進行排序。對模板類中的成員函數使用`std :: sort`
使用的typedef的想法從另一個Stackoverflow Post
反正來了,這裏是代碼:
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
預先感謝您!
對不起,作爲一個新手,第二個選項將如何實施? 謝謝! – Yuuta
@Yuuta:查看更新的答案。 –
非常感謝! – Yuuta