0

我不知道如何修复编译错误 C2664,这让我整晚都发疯了!错误来自对 qsort() 的调用。我想对存储在 radioIDs 指向的数组中的 ID2IX 数组进行排序:

 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 );

我从中得到的错误是:

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

我到底做错了什么?

编辑:谢谢大家。我们的 C/ASM 编码人员,我们不会打扰 'bout no 该死的const。

4

3 回答 3

2

sort_by_radioID将 的签名更改为:

int __cdecl sort_by_radioID(const void* 一, const void* 二)

并确保您投射到const ID2IX*函数内部。

(如果 __cdecl 是默认调用类型,你可以跳过它。尝试不使用它,看看它是否编译)

于 2012-02-22T10:39:46.087 回答
1

试试签名sort_by_radioID ( const ID2IX * one , const ID2IX * two)

于 2012-02-22T10:39:53.340 回答
1

您的比较函数签名错误(qsort 需要不同类型的函数指针)。

Solution: change your function to:       int sort_by_radioID ( const void* one , const void*); Remember also to change casting of pointers in the body of your comparison function to 'const ID2DX*'.

于 2012-02-22T10:41:47.760 回答