无法理解的 VC++6 编译错误 C2664

发布于 2025-01-07 14:10:15 字数 1235 浏览 1 评论 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 编码员,我们不会为任何该死的const而烦恼。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

同展鸳鸯锦 2025-01-14 14:10:15

sort_by_radioID 的签名更改为:

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

并确保在函数内强制转换为 const ID2IX*

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

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)

私野 2025-01-14 14:10:15

尝试签名sort_by_radioID(const ID2IX *一,const ID2IX *二)

try the signature sort_by_radioID ( const ID2IX * one , const ID2IX * two)

兲鉂ぱ嘚淚 2025-01-14 14:10:15

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

解决方案:将您的功能更改为:
      int sort_by_radioID ( const void* one , const void*);
还请记住更改比较函数体内的指针转换
到“const ID2DX*”。

Your comparison function has wrong signature (qsort expects different type of function pointer).

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*'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文