I have these two structures...
typedef struct{
MY_SECOND_STRUCT s1;
}MY_FIRST_STRUCT;
typedef struct{
int s1;
}MY_SECOND_STRUCT;
I prefer this order, I dont want to switch them. But compiler dont know MY_SECOND_STRUCT at the moment and I get error
error: expected specifier-qualifier-list before 'MY_SECOND_STRUCT'
I´ve tried add declaration to the top
struct MY_SECOND_STRUCT;
also change definition to
typedef struct{
struct MY_SECOND_STRUCT s1;
}MY_FIRST_STRUCT;
but it didnt help.
That order is not possible. You have to switch them.
However, if you declare the member as pointer, then switching is not required:
struct MY_SECOND_STRUCT; //forward declaration is required though
typedef struct{
MY_SECOND_STRUCT * s1; //now its a pointer
}MY_FIRST_STRUCT;
Or, in C++ you can use template as:
template<typename MY_SECOND_STRUCT>
struct MY_FIRST_STRUCT_T
{
MY_SECOND_STRUCT s1;
};
struct MY_SECOND_STRUCT
{
int s1;
};
And when you want to use MY_FIRST_STRUCT
, just use this typedef
:
typedef MY_FIRST_STRUCT_T<MY_SECOND_STRUCT> MY_FIRST_STRUCT;
Use MY_FIRST_STRUCT
now. :-)