简体   繁体   中英

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

here is my function in class 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(ap, len1+len2, sizeof(char *),pstrcmp);

which ap is a pointer array of suffix

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

 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.

You have two solutions to this problem:

  • Move the declaration of pstrcmp out of the SuffixArray class, or
  • Declare pstrcmp static in the class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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