Search code examples
cvisual-c++c2664

Incomprehensible VC++6 compile error C2664


I can't figure out how to fix a compile error C2664, which has driven me crazy all night! The error arises from a call to qsort(). I want to sort an array of ID2IX stored in the array pointed by radioIDs:

 typedef struct id2ix { // struct maps radio id to array index
         int id;    // radio id
         int ix;
       } ID2IX;

  ID2IX      *RadioIDs   = NULL; // radio IDs             integer
.....
  RadioIDs = (ID2IX*) malloc( totRadios * sizeof( ID2IX ));
  if ( RadioIDs == NULL ) {
    return FALSE;
  }
.....    
    // the qsort compar function 
    int   // sort the id2ix array by radioID
    //sort_by_radioID ( ID2IX*one , ID2IX*two) {  // tried this signature
      sort_by_radioID ( void*one , void*two) {    // tried this signature, also
        return ((ID2IX*)one)->id - ((ID2IX*)two)->id;
    }

    // call to qsort that will not compile
    qsort( RadioIDs, totRadios, sizeof(ID2IX), sort_by_radioID );

The error I get out of this is:

Objects.cpp(295) : error C2664: 'qsort' : cannot convert parameter 4
     from 'int (void *,void *)'
       to 'int (__cdecl *)(const void *,const void *)'
None of the functions with this name in scope match the target type

What the heck am I doing wrong?

EDIT: Thanks, everybody. Us C/ASM coders, we don't bother 'bout no damn const.


Solution

  • Change sort_by_radioID's signature to:

    int __cdecl sort_by_radioID(const void* one, const void* two)

    And make sure you cast to const ID2IX* inside the function.

    (if __cdecl is the default call type, you can skip it. Try without it and see if it compiles)