The following code compiles in C, using ARMCC under Keil, However fails to compile in C++, using G++ under eclipse. The original code had some const keywords but that seems to be causing another, less important issue, so I removed them for now.
struct MENU
{
struct MENU * NextMenu;
struct MENU * PrevMenu;
void (* InitFunction)(void);
};
typedef struct MENU MENU_T;
MENU_T MENU_A; // <- this forward declaration is needed for circular reference between structs
MENU_T MENU_B;
MENU_T MENU_C;
MENU_T
MENU_A = // <- However redefinition error occurs here
{
.NextMenu = &MENU_B,
.PrevMenu = &MENU_C,
.InitFunction = 0,
};
MENU_T
MENU_B =
{
.NextMenu = &MENU_C,
.PrevMenu = &MENU_A,
.InitFunction = 0,
};
MENU_T
MENU_C =
{
.NextMenu = &MENU_A,
.PrevMenu = &MENU_B,
.InitFunction = 0,
};
As far as I can tell, the first line is a declaration and not a definition, so the "error redefinition" should not occur...
With MENU_T MENU_A;
, you do not "forward declare" a variable named MENU_A
, you rather define it. Hence, when you later write MENU_T MENU_A = { ... }
, you get an error indicating that you redefine a variable with the same name MENU_A
.
For just declaring a variable (without defining it), use keyword extern
:
extern MENU_T MENU_A; // declaration of MENU_A, not a definition
extern MENU_T MENU_B;
extern MENU_T MENU_C;
MENU_T MENU_A =
{
.NextMenu = &MENU_B,
.PrevMenu = &MENU_C,
.InitFunction = 0,
};
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.