進出口工作試圖用函數指針的力量,這一切都很好,直到我試圖使函數指針使用第二個參數爲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