无法理解的 VC++6 编译错误 C2664
我不知道如何修复编译错误 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将
sort_by_radioID
的签名更改为:并确保在函数内强制转换为
const ID2IX*
。(如果 __cdecl 是默认调用类型,则可以跳过它。尝试不使用它,看看是否可以编译)
Change
sort_by_radioID
's signature to: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)
尝试签名
sort_by_radioID(const ID2IX *一,const ID2IX *二)
try the signature
sort_by_radioID ( const ID2IX * one , const ID2IX * two)
您的比较函数的签名错误(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*'.