请帮助我确定菜单项的函数指针的问题是给出错误“从不兼容的指针类型初始化”
这是头文件
/**
* Represents a function that can be selected from the list of
* menu_functions - creates a new type called a menu_function.
*/
void displayItems(VmSystem * system);
typedef void (*MenuFunction)(VmSystem *);
/**
* Represents a menu item to be displayed and executed in the program.
**/
typedef struct menu_item
{
char text[MENU_NAME_LEN + NULL_SPACE];
MenuFunction function;
} MenuItem;
void initMenu(MenuItem * menu);
MenuFunction getMenuChoice(MenuItem * menu);
MenuItem menu[NUM_MENU_ITEMS];
主菜单文件
typedef enum boolean
{
FALSE = 0,
TRUE
} Boolean;
void initMenu(MenuItem * menu)
{
/* Strings names of menu items */
char * menu_items[] = {
"Display Items",
"Purchase Items",
"Save and Exit",
"Add Item",
"Remove Item",
"Display Coins",
"Reset Stock",
"Reset Coins",
"Abort Program"
};
菜单项的功能指针
Boolean(*MenuFunction[])(VmSystem *) = {
displayItems, /*Here i got the error */
purchaseItem,
saveStock,
addItem,
removeItem,
displayCoins,
resetStock,
resetCoins,
abortProgram
};
你有两个问题。
首先:
typedef void (*MenuFunction)(VmSystem *);
其次是
Boolean(*MenuFunction[])(VmSystem *) = { ... };
两个不同的东西,同一个名字。
然后对于您的错误:您在评论中声明,例如displayItem
匹配函数 type MenuFunction
,并且它返回void
。问题是数组MenuFunction
(看看这有多混乱?)是一个函数返回的数组Boolean
。两种类型不兼容,必须换一种。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句