我得到了一個如下所示的函式介面:
typedef enum order (*cmp_fun)(const void *x, const void *y);
bool tree_member(cmp_fun, const void *, const struct tree *);
const struct tree *tree_insert(cmp_fun, const void *, const struct tree *);
當我嘗試為函式指標命名并實作該函式時,出現編譯器錯誤,但我不確定自己做錯了什么:
bool tree_member(order (*funcptr), const void* test_value, const struct tree* my_tree){... ///?
const struct tree* tree_insert(int (*funcptr), const void* insert_value, const struct tree* my_tree){... //???
有一些關于接受函式指標作為引數的函式宣告,我不理解
In file included from tree-examples.c:21:
tree.c:15:18: error: unknown type name ‘order’
15 | bool tree_member(order (*funcptr), const void* test_value, const struct tree* my_tree){
| ^~~~~
tree.c:22:20: error: conflicting types for ‘tree_insert’
22 | const struct tree* tree_insert(int (*funcptr), const void* insert_value, const struct tree* my_tree){
| ^~~~~~~~~~~
我嘗試了許多變體:
bool tree_member(cmp_fun (*funcptr), const void* test_value, const struct tree* my_tree){
const struct tree* tree_insert(cmp_fun (*funcptr), const void* insert_value, const struct tree* my_tree){
uj5u.com熱心網友回復:
編譯器告訴您它不能識別order為型別名稱。您需要撰寫enum order而不僅僅是order.
查看代碼的那部分,我注意到您為tree_member函式的第一個引數使用了錯誤的型別。介面告訴你,的第一個引數tree_member必須有型別cmp_fun,但你不小心讓它有型別order *。
編譯器還告訴您,tree_insert您撰寫的實作型別錯誤。仔細查看 的第一個引數tree_insert。cmp_fun根據界面應該是 a ,但是您不小心將第一個引數int *改為an 。
提示:無需從頭輸入函式實作,您只需復制給定的宣告,替換;為{ },并為缺少名稱的任何引數添加名稱。按照此提示,您將獲得以下代碼作為起點:
bool tree_member(cmp_fun cmp, const void * ptr,
const struct tree * my_tree)
{
}
const struct tree * tree_insert(cmp_fun cmp, const void * ptr,
const struct tree * my_tree)
{
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/363015.html
標籤:C
上一篇:試圖擺脫fgetswhile回圈
