Cli*_*ter 6 c parameters struct pointers function
这是代码:
typedef struct {
void (*drawFunc) ( void* );
} glesContext;
void glesRegisterDrawFunction(glesContext *glesContext, void(drawFunc*)(glesContext*));
Run Code Online (Sandbox Code Playgroud)
对于最后一行,我收到错误消息:"预期')'''''令牌"之前
为什么?
你有正确的方法来做你的功能指针struct(所以很荣幸,所以很多人都错了).
然而,你周围的交换drawFunc,并*在你的函数定义中,这是一个原因,为什么编译器抱怨.另一个原因是您使用相同的标识符作为类型和变量.您应该为两个不同的事物选择不同的标识符.
请改用:
void glesRegisterDrawFunction(glesContext *cntxt, void(*drawFunc)(glesContext*));
^^^^^^^^^
note here
Run Code Online (Sandbox Code Playgroud)
一种解决方案是添加一个指向函数typedef的指针,如下所示:
typedef struct {
void (*drawFunc) ( void* );
} glesContext;
// define a pointer to function typedef
typedef void (*DRAW_FUNC)(glesContext*);
// now use this typedef to create the function declaration
void glesRegisterDrawFunction(glesContext *glesContext, DRAW_FUNC func);
Run Code Online (Sandbox Code Playgroud)