2012-02-22 73 views
0

我無法弄清楚如何解決編譯錯誤C2664,這讓我瘋狂的整晚!調用qsort()會導致錯誤。我要排序存儲在數組中ID2IX的數組,radioIDs指出:不可理解的VC++ 6編譯錯誤C2664

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。

+1

如果你添加編譯器告訴你缺少的const,會發生什麼? – Mat 2012-02-22 10:38:09

+1

嘗試使用簽名'int sort_by_radioID(const void * one,const void * two)'。 – Naveen 2012-02-22 10:38:22

+1

檢查你的默認調用conv以及,如果它不是'__cdecl',你需要明確地使用'__cdecl'作爲函數 – Necrolis 2012-02-22 10:41:32

回答

2

變化sort_by_radioID的簽名:

INT __cdecl sort_by_radioID(常量無效*之一,常量無效*二)

並確保你投以const ID2IX*函數內。

(如果__cdecl是默認的呼叫類型,你可以跳過它。沒有它嘗試,看看它是否編譯)

1

嘗試簽名sort_by_radioID (const ID2IX * one , const ID2IX * two)

1

您的比較函數簽名錯誤(快速排序預計不同類型的函數指針)。

解決方案:將您的功能更改爲: int sort_by_radioID(const void * one,const void *); 還要記住,將比較函數 的指針轉換爲'const ID2DX *'。