2016-11-29 61 views
0

进出口工作试图用函数指针的力量,这一切都很好,直到我试图使函数指针使用第二个参数为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 *”

+0

它应该是void(* move)(struct UnitTag,int);我不知道如何失效(*移动)(单位);正在为你工作。 – MayurK

+0

@MayurK,它甚至给出了相同的错误使用时 “空隙(*移动)(UnitTag,INT);” – LamaCoder

+0

@LamaCoder你错过了'struct'关键字。 'void(* move)(struct UnitTag,int);' – aschepler

回答

3

如果我得到你,你可以简单地使用struct关键字:

#include <stdio.h> 

typedef struct UnitTag { 
    int x; 
    int y; 
    void (*move)(struct UnitTag, int); 
} Unit; 

void Test (struct UnitTag test1, int test2) 
{ 
    printf("Test1.x: %d\n", test1.x); 
    printf("Test1.y: %d\n", test1.y); 
    printf("Test2 : %d\n", test2); 
} 

int main(void) 
{ 
    Unit units[100]; 

    units[0].move = Test; 
    units[0].x = 1; 
    units[0].y = 2; 

    units[0].move(units[0], 3); 
} 

输出:

Test1.x: 1 
Test1.y: 2 
Test2 : 3 

如果您想通过referebce传递结构,简单地说:

#include <stdio.h> 

typedef struct UnitTag { 
    int x; 
    int y; 
    void (*move)(struct UnitTag*, int); 
} Unit; 

void Test (struct UnitTag *test1, int test2) 
{ 
    test1->x = 4; 
    test1->y = 5; 
} 

int main(void) 
{ 
    Unit units[100]; 

    units[0].move = Test; 
    units[0].x = 1; 
    units[0].y = 2; 

    units[0].move(&units[0], 3); 

    printf("units[0].x: %d\n", units[0].x); 
    printf("units[0].y: %d\n", units[0].y); 
} 

输出为:

units[0].x: 4 
units[0].y: 5 
+0

似乎工作,但是我会修改结构,我是通过引用传递这是。 – LamaCoder

+0

@LamaCoder我编辑。 – LPs

+0

我用代码中的Unit替换UnitTag,并且还删除参数中的Test关键字的struct关键字。它似乎工作正常,只是为了提高可读性,这是不好的做法呢? – LamaCoder

1

你使用它之前需要的原型装置。

#include <stddef.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

typedef struct UnitTag Unit; 

typedef struct UnitTag { 
    int x; 
    int y; 
    void (*move)(Unit, int); 
} Unit; 

int main(void) 
{ 
    return 0; 
} 

之后澄清你想要做什么。给Unit赋一个指针可能更有意义,这样返回void的move命令可以改变你的对象。

#include <stddef.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

typedef struct UnitTag Unit; 

typedef struct UnitTag { 
    int x; 
    int y; 
    void (*move)(Unit *, int); 
} Unit; 

Unit test; 

/* some function that corresponds to the interface */ 
void myMove(Unit *u, int i) 
{ 
    u->x = u->x + i; 
} 

int main(void) 
{ 
    /* initialize test struct */ 
    test.x = 0; 
    test.y = 0; 
    test.move = myMove; 

    test.move(&test, 5); 

    printf("Values after move are (x, y) = (%i, %i).\n", test.x, test.y); 

    return 0; 
} 
+0

请告诉你如何失效(*移动)(单位);工作中? – MayurK

+0

它解决了这个错误,但是现在我不能使用和我以前一样的函数,错误是: 错误:'units [i] .move' 单位的参数1的不兼容类型[i] .move &units [i],cycles); ^ 注意:期望'单位',但参数的类型是'结构UnitTag *' 现在我遇到了这个错误无处不在我使用这个。数组的定义是:Unit units [UNITCOUNT]; – LamaCoder

+1

您应该在您使用该结构的地方发布代码。 – LPs