警告:从不兼容的指针类型初始化[-Wincompatible指针类型]
请帮助我确定功能指针的问题,因为菜单项出现错误“从不兼容的指针类型初始化” 这是头文件警告:从不兼容的指针类型初始化[-Wincompatible指针类型],c,gcc,C,Gcc,请帮助我确定功能指针的问题,因为菜单项出现错误“从不兼容的指针类型初始化” 这是头文件 /** * 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)(VmSys
/**
* 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
与函数类型menuffunction
匹配,并返回void
。问题是数组menuffunction
(看看这有多混乱?)是一个返回布尔值的函数数组。这两种类型不兼容,您必须更改其中一种。什么是显示项,例如displayItems
?它是否与类型别名菜单功能
匹配?什么是布尔值?它是否等于void
(这是MenuFunction
函数的返回类型)?请确保您在问题中提供了一个!displayItems和所有其他项无效displayItems(VmSystem*system),布尔值为枚举,菜单函数为:typedef void(*menuffunction)(VmSystem*);与您的问题无关,但请不要使用您自己的布尔类型。C已经从C99开始了谢谢你帮了我很多忙,我几乎花了两个小时,新手:)