C 字段的类型不完整

C field has incomplete type

我有以下头文件:

 #ifndef SERVER_STRUCTURES_H
 #define SERVER_STRUCTURES_H

typedef struct game {
  int id;
  struct player player1;
  struct player player2;
  struct game *next;
} game_t;

typedef struct player {
  int id;
  int score;
  struct player *player1;
  struct game *g ;
} player_t;

#endif

我收到错误:字段 'player1' 的类型不完整 struct player player1

字段 'player2' 的结构类型 struct player player2 不完整。

错误是什么?谢谢!

声明必须在使用它们的地方之前,所以两个声明应该互换。 要分配 player1player2,编译器将需要 struct player.

的完整声明

然后,你应该告诉编译器struct game稍后声明。 这些信息足以创建 "pointer to something".

#ifndef SERVER_STRUCTURES_H
#define SERVER_STRUCTURES_H

struct game;

typedef struct player {
  int id;
  int score;
  struct player *player1;
  struct game *g ;
} player_t;

typedef struct game {
  int id;
  struct player player1;
  struct player player2;
  struct game *next;
} game_t;

#endif