平常使用时,在写sort函数的比较函数时,通常都是:
bool cmp(int i1,int i2){
return i1 < i2;
}
void foo(){
sort(vec.begin(),vec.end(),cmp);
}
习惯用法后,在一些类中,同样这样写就出现了错误。
class InClass{
bool cmp(int i1,int i2){
return i1 < i2;
}
void foo(){
sort(vec.begin(),vec.end(),cmp);
}
};
出现报错:
reference to non-static member function must be called sort(vec.begin(),vec.end(),cmp);
写在类内时,cmp函数需要是静态函数,要加上static。