c++ qsort中非静态成员函数的无效使用

invalid use of non-static member function in qsort C++

本文关键字:无效 函数 静态成员 qsort c++      更新时间:2023-10-16

下面是我在类SuffixArray中的函数:

int pstrcmp(const void *a, const void *b) {
return strcmp((const char *)*(char **)a, (const char *)*(char **)b);
}

在qsort中使用了这个比较函数:

qsort(ap, len1+len2, sizeof(char *),pstrcmp);

,其中ap是后缀

的指针数组

当我编译它时,有一个错误:非静态成员函数

的无效使用

我用notepad++来编译它,它提供了

 error: cannot convert 'SuffixArray::pstrcmp' from type 'int (SuffixArray::)(const void*, const void*)' to type 'int (*)(const void*, const void*)'
 qsort(ap, len1+len2, sizeof(char *),pstrcmp);
有人能帮我吗?

在c++中,您需要向qsort传递一个独立函数或静态成员函数(与非静态成员函数相反),因为非静态成员函数的调用约定需要传递一个实例。

这个问题有两个解决方案:

  • pstrcmp的声明移出SuffixArray类,或者
  • 在类中声明pstrcmp静态