Typedef函数指针编译问题 [英] Typedef function pointer compilation problem

查看:463
本文介绍了Typedef函数指针编译问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用结构指针的参数声明一个函数指针(这也是一个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 a typedef 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆