1
votes

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?

1
You tagged this with the C++ tag. If this is C++ then avoid C-style casts, use C++ casts to make your intent more clear. I don't like the way you're seemingly applying unnecessary casts to your arguments. - Dai
@Dai OP tagget it with C, I re-tagged for C++, because it's clearly not a C question. - Sergey Kalinichenko

1 Answers

2
votes

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.