我有以下头文件:
#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。
怎么了?谢谢!
声明必须在使用它们的位置之前,因此应交换两个声明。要分配player1
和player2
,编译器将要求完整声明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