Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
};
C++
typedef struct A myStruct;


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?
Posted
Updated 24-Jul-16 23:32pm

1 solution

I'm missing a typedef in your code:
C++
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;


However, you may also use a typedef'ed struct instead:
C++
typedef struct 
{
    int Z;
} myStruct;
typedef void (*callback_fn_ptr)(myStruct *ptr);

// ...
callback_fn_ptr my_fun_ptr;


Both above snippets compile with GCC.
 
Share this answer
 
Comments
chandrAN2& 25-Jul-16 6:36am    
Thanks.. with typedef only i got the error..i forget to add typedef,sorry

typedef void (*callback_fn_ptr)(myStruct *ptr); //error "typedef 'callback_fn_ptr' is initialized (use decltype instead)
typdedef void (*callback_fn_ptr)(struct A *ptr); //no error
chandrAN2& 25-Jul-16 6:37am    
I dont understand the reason for the error? What the error implies?
Jochen Arndt 25-Jul-16 7:11am    
Hmm. As I wrote I have compiled both snippets from my solution and did not get an error.

I assumed that the error was sourced by the missing typedef for your function declaration.

This is a function declaration. The compiler expects that there is a corresponding function definition somewhere. The definition might be located in another module and is resolved during the linking process:

void (*callback_fn_ptr)(myStruct *ptr);

This is a typedef (an alias) to be used later with declarations and definitions:

typedef void (*callback_fn_ptr)(myStruct *ptr);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900