Domanda

i have simple c++ class where there i need to do sort array , here is what i have :

void GameController::sortArray(CCArray *&sameRowGemArray)
{
        qsort(sameRowGemArray->data->arr, sameRowGemArray->data->num, sizeof(long), &GameController::comperator);


}

int  GameController::comperator(const void * a, const void * b)
{
  /*
  ..didn't implement logic first i need it to compile 
  */
    return 1;
}

but im getting compilation error that say :

error C2664: 'qsort' : cannot convert parameter 4 from 'int (__thiscall GameController::* )(const void *,const void *)' to 'int (__cdecl *)(const void *,const void *)'

isnt it pointer to function in the where is the comperator?

È stato utile?

Soluzione

Let's try to understand the error message.

int (__thiscall GameController::* )(const void *,const void *)

You are trying to pass address of a member function whose return type is int and takes inputs of type const void *. The word __thiscall signifies that you are passing a C++ member function and has the hidden this argument during the calls.

int (__cdecl *)(const void *,const void *)

__cdecl * signifies that it expects a plain C function as a call back. Notice that it has no knowledge about the hidden argument this.

So, you are a passing a C++ member function while the qsort expects a C type function as a predicate.

You can convert the function to static which will resolve the issue.

Altri suggerimenti

You need to pass a pointer to either a static member function or a non member function. Either make comperator static or remove it from the class GameController (for example, place it in an unnamed namespace within your implementation).

This is because the signature for a member function is different; it requires a object to be called on.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top