C字段的类型不完整

问题描述 投票:-2回答:1

我有以下头文件:

 #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。

怎么了?谢谢!

c struct typedef heading
1个回答
0
投票

声明必须在使用它们的位置之前,因此应交换两个声明。要分配player1player2,编译器将要求完整声明struct player

然后,您应该告诉编译器以后将声明struct game。这足以创建“指向某物的指针”。

#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
© www.soinside.com 2019 - 2024. All rights reserved.