进出口工作试图用函数指针的力量,这一切都很好,直到我试图使函数指针使用第二个参数为int类型。函数指针不是INT
下面的代码生成一个错误,其上显示下面 在头文件:
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct UnitTag {
int x;
int y;
void (*move)(Unit, int);
} Unit;
错误:
error: expected ‘)’ before ‘int’
void (*move)(Unit, int);
^
空隙(*移动)(单位);工作都很好,这让我惊讶地发现,如何添加参数会导致错误。
我打电话给我的结构在C文件,通过包括头,然后做: 单位单元[UNITCOUNT]; 单元[0] .move(&单元[0],1);
更新:
补充说:
typedef struct UnitTag Unit
导致错误dissapear,不过,我可以像以前一样不再使用该功能。
error: incompatible type for argument 1 of ‘units[i].move’
units[0].move(&units[0], 0);
^
注:应为“单位”,但参数的类型的“结构UnitTag *”
它应该是void(* move)(struct UnitTag,int);我不知道如何失效(*移动)(单位);正在为你工作。 – MayurK
@MayurK,它甚至给出了相同的错误使用时 “空隙(*移动)(UnitTag,INT);” – LamaCoder
@LamaCoder你错过了'struct'关键字。 'void(* move)(struct UnitTag,int);' – aschepler