【发布时间】:2014-06-10 02:41:52
【问题描述】:
我正在通过编写国际象棋应用程序来学习 C,但我遇到了循环引用的问题。我的linkedList.h 看起来像这样:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif /* LINKEDLIST_H */
#include <stdlib.h>
#include "squares.h"
typedef struct node {
tSquare c;
struct node * next;
} node_square;
void createEmptyList(node_square* n);
int isEmptyList(node_square* n);
int insertAtBeginning(node_square** n, tSquare c);
void print_list(node_square * head);
在我的 squares.h 中,我想包含linkedList.h 功能,这样我就可以返回一个受到其中一侧(黑色或白色)威胁的方块的链接列表:
#ifndef SQUARES_H
#define SQUARES_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif /* SQUARES_H */
typedef struct {
int file;
int rank;
} tSquare;
node_square* listOfThreatenedSquares(tColor color); <--- undefined types in compilation time
我读到我应该使用前向引用;我正在尝试使用它,以便在 squares.h 文件中我可以使用类型 node_square 和 tColor(在另一个名为 pieces.h 的文件中定义),但无论我如何声明类型,它都无法正常工作。我想这有点像
typedef struct node_square node_square;
typedef struct tColor tColor;
在 squares.h 中。想法?
【问题讨论】:
标签: c struct typedef forward-declaration