[英]function pointer in a struct with same argument type
I am trying to make a struct which has a function pointer for a function that takes the same struct as an argument. 我正在尝试创建一个结构,该结构具有一个函数的函数指针,该函数使用与参数相同的结构。 I have this at the moment.
我现在有这个。
typedef struct sharedData
{
sem_t* forks;
int id;
void (*forkFunc)(sharedData*);
};
I am getting errors like 我收到的错误就像
error: expected ')' before '*' token
错误:在'*'标记之前预期')'
and warnings like 和警告一样
warning: no semicolon at end of struct or union
warning: useless storage class specifier in empty declaration
what am I doing wrong here? 我在这做错了什么?
The problem is that when you're using typedef struct
to introduce a new struct
that doesn't require the struct
keyword, you cannot refer to the typedef
-ed name inside the declaration of the struct
. 问题是当你使用
typedef struct
来引入一个不需要struct
关键字的新struct
,你不能在struct
的声明中引用typedef
-ed名称。 Instead, you need to use the full name for the struct. 相反,您需要使用结构的全名。 For example:
例如:
typedef struct sharedData
{
sem_t* forks;
int id;
void (*forkFunc)(struct sharedData*);
};
Also, your typedef
statement is currently invalid because you haven't given a name by which to call struct sharedData
. 此外,您的
typedef
语句当前无效,因为您没有给出用于调用struct sharedData
的名称。 One way to fix this would be as follows: 解决这个问题的一种方法如下:
typedef struct sharedData
{
sem_t* forks;
int id;
void (*forkFunc)(struct sharedData*);
} sharedData;
Now, you can refer to the struct by the full name struct sharedData
or by the shorthand name sharedData
. 现在,您可以通过全名
struct sharedData
或简写名称sharedData
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.