简体   繁体   English

在Qsort C ++中无效使用非静态成员函数

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

here is my function in class SuffixArray: 这是我在SuffixArray类中的函数:

int pstrcmp(const void *a, const void *b) {

return strcmp((const char *)*(char **)a, (const char *)*(char **)b);
}

I used this comparison function in qsort: 我在qsort中使用了此比较功能:

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

which ap is a pointer array of suffix 其中ap是后缀的指针数组

When I compile it, there is an error: invalid use of non-static member function 当我编译它时,出现一个错误:非静态成员函数的无效使用

and I use notepad++ to compile it, it provide that 我使用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);

is there anyone can help me? 有没有人可以帮助我?

In C++ you need to pass a free-standing function or a static member function (as opposed to a non-static member function) to qsort , because calling conventions of non-static member functions require an instance to be passed. 在C ++中,您需要将独立函数或静态成员函数(与非静态成员函数相对)传递给qsort ,因为非静态成员函数的调用约定需要传递实例。

You have two solutions to this problem: 您有两种解决此问题的方法:

  • Move the declaration of pstrcmp out of the SuffixArray class, or pstrcmp的声明移出SuffixArray类,或者
  • Declare pstrcmp static in the class. 在类中声明pstrcmp static。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM