[英]“Candidate function not viable;” Issues with porting a C library to C++
我試圖在C ++實現中使用ac庫,但經常遇到與“ cantidate函數不可行”有關的錯誤,而且似乎認為該庫中的函數無法與我的代碼中提供的參數一起使用。 關鍵是,我的代碼從字面上看是一個舊的實現(在c中),當在c中編譯時,它可以很好地工作。 有沒有辦法將其作為我的c ++環境的一部分進行編譯?
這是我得到的錯誤:
testbed/des-lsr_routinglogic.cpp:20:27: error: no matching function for call to 'dessert_periodic_add'
periodic_refresh_nh = dessert_periodic_add(refresh_list, NULL, NULL, &refresh_neighbor_t);
^~~~~~~~~~~~~~~~~~~~
/usr/local/include/dessert.h:880:21: note: candidate function not viable: no known conversion from 'dessert_per_result_t ()' to 'dessert_periodiccallback_t *' (aka 'dessert_per_result_t (*)(void *, struct
timeval *, struct timeval *)') for 1st argument
dessert_periodic_t* dessert_periodic_add(dessert_periodiccallback_t* c, void* data, const struct timeval* scheduled, const struct timeval* interval);
^
從以下代碼行:
periodic_refresh_nh = dessert_periodic_add(refresh_list, NULL, NULL, &refresh_neighbor_t);
使用該庫的摘錄:
dessert_periodic_t* dessert_periodic_add(dessert_periodiccallback_t* c, void* data, const struct timeval* scheduled, const struct timeval* interval);
dessert_periodic_t* dessert_periodic_add_delayed(dessert_periodiccallback_t* c, void* data, int delay);
int dessert_periodic_del(dessert_periodic_t* p);
void dessert_register_ptr_name(void* ptr, const char* name);
const char* dessert_ptr2name(void* ptr);
我認為對於有組合c / c ++經驗的人來說,編譯器的輸出應該是不言而喻的,但是對我來說,這看起來像是胡言亂語D:
謝謝,有人指出,我遺漏了一些重要的定義。 refresh_list的定義:
dessert_per_result_t refresh_list(void *data, struct timeval *scheduled, struct timeval *interval) {
pthread_rwlock_wrlock(&pp_rwlock);
node_neighbors_t *neighbor = dir_neighbors_head;
while (neighbor) {
if (neighbor->entry_age-- == 0) {
node_neighbors_t* el_to_delete = neighbor;
HASH_DEL(dir_neighbors_head, el_to_delete);
free(el_to_delete);
} else {
neighbor->weight = 1;
}
neighbor = neighbor->hh.next;
}
pthread_rwlock_unlock(&pp_rwlock);
return 0; }
dessert_per_result_t的定義:
typedef enum _dessert_periodic_results { DESSERT_PER_KEEP = 0, DESSERT_PER_UNREGISTER = 1 } dessert_per_result_t;
dessert_periodiccallback_t的定義很奇怪; 我將嘗試發布它。
C ++在函數聲明方面比C語言更為嚴格。在這種情況下,函數refresh_list
聲明為dessert_per_result_t refresh_list()
,但是dessert_periodic_add
的第一個參數必須是具有明確指定參數類型的函數的指針。
解決此問題的最佳方法是添加一個完整的refresh_list
聲明,以匹配dessert_periodic_add
期望,代替其簡短聲明。
編譯器消息顯示編譯器認為refresh_list
是不帶任何參數並返回dessert_per_result_t
。
您發布的定義不同意。
這表明存在單獨的refresh_list
聲明,該聲明與定義不一致。
錯誤消息中顯示的refresh_list
的聲明是這樣的:
dessert_per_result_t refresh_list();
這意味着在C ++和C中有兩件事。
在C ++中,這意味着refresh_list
接受零個參數。
在C語言中,這意味着refresh_list
接受未指定類型的數量的未指定參數。
因此,為什么它可以在C中工作,但在C ++中不允許使用指向refresh_list
的指針,而在該指針中,則需要一個指向dessert_per_result_t(void *, struct timeval *, struct timeval *)
的指針。
也可以看看:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.