在类中使用自定义排序时编译错误

Compilation Error using Custom Sort inside Class

本文关键字:排序 编译 错误 自定义      更新时间:2023-10-16

我正在尝试在类中使用自定义排序, 如sort(arr,arr+n,cust);在类函数内。 自定义排序如下所示:

bool cust (int a, int b) {
return pos[a]<pos[b];
}

其中pos是类中的数组。 但它不编译,并给出编译错误

"无效使用非静态成员函数" 如何摆脱编译错误。

我的代码 -> https://pastebin.com/W1zw0A5s

我试过写:

static bool cust (int a, int b) {
/* same code */
} 

这没有帮助。

根据 pastebin 链接,此函数是类中的成员函数。您需要将this指针绑定到它才能使用它:

sort(sa,sa+n, [this](int a, int b){ return this->sufCmp(a,b); } );