c - 错误 c2371 结构 <type> 重新定义

标签 c syntax-error visual-studio-debugging

因此,我在 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 **变量”,而不是“struct CELL **变量”。

编辑:我向 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 语法错误

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

最佳答案

头文件应以 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_H现已定义,并且 union.h 中不包含任何内容。因此,递归包含到此为止。值得一提的是,整个头文件union.h应包含在 #ifndef UNION_H ... #endif 中.

出现了一个新问题:if union.h首先包含,AI.h包含在结构体定义之前 CELL 。但功能在 AI.h操作CELL** !为了解决这个问题,我们引入 CELL前向声明AI.h (参见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 的内容不会被包含两次。

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

关于c - 错误 c2371 结构 <type> 重新定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35020328/

相关文章:

c - 将十六进制打印为字符串

ruby-on-rails - Rails教程第11章错误 "uninitialized constant User::Relationships"

jquery - 跨域ajax的问题

visual-studio-2010 - debugattach.aspx 是什么,为什么服务器找不到?

c++ - 调试显示不正确值的 64 位 DLL 检查器

c - SSE SIMD 代码中的性能问题

c - 程序打印全 1?

c - 从 RPI 到 HMCL3855L 的 I2C 通信故障排除

花括号语法错误后的javascript点? : {num:1}. 编号

visual-studio - Visual Studio : No disassembly available