【问题标题】:error c2371 struct <type> redefinition错误 c2371 结构 <类型> 重新定义
【发布时间】:2016-05-03 09:34:41
【问题描述】:

所以,我在 Visual Studio 中多次收到上述错误。这是我的代码: Union.h

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef AI_H
#define AI_H
#include "AI.h"
#endif

#ifndef UI_H
#define UI_H
#include "UI.h"
#endif

typedef struct BOARD_CELL {
    int player, wall, steps;
}CELL;

typedef struct {
    int x, y;
}COORD;

AI.h

#include "union.h"
void pathfind(CELL **a, int n);
void ai(CELL **a);
void genmove(CELL **a); 

UI.h

#include "union.h"

void cmdtoAct(char* c, CELL **a, char player_counter, COORD white_vertex, COORD black_vertex);
void placeWall( CELL **a, char* str, char* str2, int n);
void playmove( CELL **a, char *colour, char *vertex, COORD player_vertex);
int pathCheck( CELL **a);
void showBoard( CELL **a);
void backdoor();
char* getCmd();

关于 .c 文件,您只需要知道每个文件都必须知道 CELL 结构和 COORDS 结构的存在,并且由于它们是类型定义的,所以当我在我的函数中使用它们时例如,作为参数,我将它们称为“CELL **variable”,而不是“struct CELL **variable”。

编辑:我为 ai.h 和 ui.h 添加了警卫,如下所示: AI.h

#ifndef AI_H
#define AI_H
#include "union.h"
#endif

void pathfind(CELL **a, int n);
void ai(CELL **a);
void genmove(CELL **a);

UI.h

#ifndef UI_H
#define UI_H
#include "union.h"
#endif

void cmdtoAct(char* c, CELL **a, char player_counter, PLAYER white, PLAYER    black);
void placeWall( CELL **a, char* str, char* str2, int n);
void playmove( CELL **a, char *colour, char *vertex, PLAYER player);
int pathCheck( CELL **a);
CELL **boardsize(struct CELL **a, int size);
void showBoard( CELL **a);
void backdoor();
char* getCmd();

现在我得到一个 C2143 SYNTAX ERROR MISSING '{' before ' * ' 还有一个 C2143 SYNTAX ERROR 在 ' * ' 之前缺少 ')'

这是怎么回事???!!!

【问题讨论】:

  • include guards 必须放在包含的头文件中,即在 Union.h 中,在 UI.h 和 AI.h 中。按原样,Union.h 包含两次!一次是 AI.h,一次是 UI.h。
  • AI.h 包含 Union.h,其中包含 AI.h?鸡还是蛋?
  • @Joyas 对stackoverflow.com/questions/17913871/… 关于 c 中结构前向声明的问题的回答可能会对您有所帮助。
  • @francis,请看编辑!我疯了!!!

标签: c syntax-error visual-studio-debugging


【解决方案1】:

头文件应以include guards 开头。例如,union.h 看起来像:

#ifndef UNION_H  //include guard
#define UNION_H  //include guard
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "AI.h"
#include "UI.h"

typedef struct BOARD_CELL {
   int player, wall, steps;
}CELL;

typedef struct {
   int x, y;
}COORD;
#endif  //include guard

这样,防止了循环包含的鸡蛋问题:union.h 包含AI.h。然后AI.h 包含union.h,但现在定义了包含保护UNION_Hunion.h 中不包含任何内容。因此,递归包含在此停止。需要指出的是,整个头文件union.h应该用#ifndef UNION_H ... #endif括起来。

出现了一个新问题:如果首先包含union.h,则在定义结构CELL 之前包含AI.h。但是AI.h 中的函数在CELL** 上运行!为了解决这个问题,让我们在AI.h中引入CELL前向声明(见C forward declaration of struct in header):

#ifndef AI_H
#define AI_H
#include "union.h"

//forward declaration of struct CELL
struct BOARD_CELL;
typedef struct BOARD_CELL CELL;

void pathfind(CELL **a, int n);
void ai(CELL **a);
void genmove(CELL **a);

#endif

再次感谢包含保护,AI.h 的内容不会被包含两次。

我没有检查上面的代码。如果您的问题没有解决,请告诉我!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多