Typedef函数指针编译问题 [英] Typedef function pointer compilation problem
问题描述
我试图用结构指针的参数声明一个函数指针(这也是一个typedefed结构)但是在编译时,我得到以下错误
typedef'callback_fn_ptr'被初始化(改为使用decltype)
myStruct未在此范围内声明
ptr未在此范围内声明。
这是我的声明。
struct A
{
int Z;
};
I am trying to declare a function pointer with argument of a structure pointer(which is also a typedefed structure) but while compiling, i am getting below error
"typedef 'callback_fn_ptr' is initialized (use decltype instead)
myStruct was not declared in this scope
ptr was not declared in this scope."
Here is my declaration.
struct A
{
int Z;
};
typedef struct A myStruct;
无效(* callback_fn_ptr)(myStruct * ptr);
在cpp文件中,函数指针变量创建如下。
callback_fn_ptr my_fun_ptr;
我尝试过:
当我将typdef结构移除到没有typedef的普通结构时,则看不到问题。
void(* callback_fn_ptr)(struct A * ptr); //编译时没问题
有人可以帮忙吗?
void (*callback_fn_ptr)(myStruct *ptr);
In cpp file, function pointer variable is created as below.
callback_fn_ptr my_fun_ptr;
What I have tried:
When i remove the typdef structure to a normal structure without typedef then issue is not seen.
void (*callback_fn_ptr)(struct A *ptr); //no problem during compilation
can someone please help?
推荐答案
我错过了<代码中code> typedef :
I'm missing atypedef
in your code:
struct A
{
int Z;
};
typedef struct A myStruct;
// Missing typedef here
//void (*callback_fn_ptr)(myStruct *ptr);
typedef void (*callback_fn_ptr)(myStruct *ptr);
// ...
callback_fn_ptr my_fun_ptr;
但是,您也可以使用 typedef
'ed struct代替:
However, you may also use a typedef
'ed struct instead:
typedef struct
{
int Z;
} myStruct;
typedef void (*callback_fn_ptr)(myStruct *ptr);
// ...
callback_fn_ptr my_fun_ptr;
以上两个片段都是用GCC编译的。
Both above snippets compile with GCC.
这篇关于Typedef函数指针编译问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!