因此,我在Visual中多次得到错误。下面是我的代码: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文件,您需要知道的是,它们中的每一个都必须知道单元格结构和COORDS结构的存在,而且由于它们是被键入的,所以当我在函数中使用它们作为参数时,我称之为"CELL **变量“,而不是"struct **变量”。
编辑:我在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语法错误,在“*”之前缺少“{”,而在“*”之前缺少一个C2143语法错误)。
怎么回事??!
发布于 2016-01-26 22:46:47
头文件应该从包括警卫开始。例如,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封装。
出现了一个新的问题:如果首先包含union.h,那么在结构CELL的定义之前就包含了AI.h。但是AI.h中的函数在CELL**上运行!为了解决这个问题,让我们向介绍一个前向声明 of CELL in AI.h(参见C在标头中结构的前向声明 ):
#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的内容不会包含两次。
我没有检查上面的代码。如果你的问题没有解决,请告诉我!
https://stackoverflow.com/questions/35020328
复制相似问题