首页 > 解决方案 > 警告:从不兼容的指针类型初始化 [-Wincompatible-pointer-types]

问题描述

请帮助我确定菜单项的功能指针的问题是给出错误“从不兼容的指针类型初始化”

这是头文件

/**
 * 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
    };

标签: cgcc

解决方案


你有两个问题。

首先:

typedef void (*MenuFunction)(VmSystem *);

其次是

Boolean(*MenuFunction[])(VmSystem *) = { ... };

两个不同的东西,同一个名字。

然后至于您的错误:您在评论中声明例如displayItem与函数类型匹配MenuFunction,并且它返回void。问题是数组MenuFunction(看看这有多混乱?)是一个函数返回的数组Boolean。这两种类型不兼容,必须换一种。


推荐阅读