C 从一个头文件到另一个头文件的结构
我要麻烦这真的相当长的时间,仍然不知道哪里可能是问题 让我们使用头文件Player.hC 从一个头文件到另一个头文件的结构,c,struct,include,typedef,header-files,C,Struct,Include,Typedef,Header Files,我要麻烦这真的相当长的时间,仍然不知道哪里可能是问题 让我们使用头文件Player.h #ifndef PLAYER_H_ #define PLAYER_H_ typedef struct player { char *name; int gameId; int socketfd; int points; int state; }player_struct; #endif /* PLAYER_H_ */ 让我们看第二个头文件Game.h #ifndef
#ifndef PLAYER_H_
#define PLAYER_H_
typedef struct player
{
char *name;
int gameId;
int socketfd;
int points;
int state;
}player_struct;
#endif /* PLAYER_H_ */
让我们看第二个头文件Game.h
#ifndef GAME_H_
#define GAME_H_
#include "Player.h"
#define NUMBEROFPLAYERS 2
typedef struct game
{
//Here
}game_struct;
#endif /* GAME_H_ */
我的typedef的目的是让类型player_struct使用如下内容:
它是源文件Player.c-这很有效
#include "Player.h"
player_struct *create_player
{
player_struct *player;
//body malloc ....
return player;
}
我用Eclipse编辑器做C和C++,我用Linux GCC编译了C项目。 为什么我不能在头文件game.h中像在player.C中一样使用类型player_struct,即使我已经包含了头文件player.h
在头文件game.h中的位置//我想在这里使用player_struct *players[NUMBEROFPLAYERS];
但它写道,无法解析dame类型
无法解析类型“player_struct”
我尝试了很多方法来编写struct和typedef,但没有任何帮助。
我真的不明白有什么想法吗?
谢谢
编辑:
我被要求将我的所有代码放在那里,以便:
当我使用类型player_struct(在第一行)时,问题出现在header Game.h中。首先 第二
/* Game.h */
#ifndef GAME_H_
#define GAME_H_
#include "Player.h"
//#include "Card.h"
#define PLAYERSLEN 2 // number of players
#define GAMESLEN 10 //number of games
#define CARDSLEN 64 //cards in one game
typedef struct game{
player_struct *players[PLAYERSLEN];//here is the problem
//card_struct *cards[CARDSLEN]; //her will be same problem
int active;
int playerOnTurn;
char *gameName;
int gameId;
}games_struct;
//typedef struct game game_struct;
//extern games_struct *games_array[GAMESLEN];
void init_game(games_struct *game);
void run_game(games_struct *game);
void *end_game(games_struct *game);
#endif /* GAME_H_ */
尝试在不使用typedef的情况下定义它们。查看
*.c
代码文件的预处理表单。在Linux上,您将使用gcc-C-E Player.C>Player.i
获取它们,然后在它们上启动寻呼机或编辑器,例如less Player.i
或emacs Player.i
顺便说一句,您的函数create\u Player
实际上并没有创建播放器。它返回一个无效的指针。请检查这个问题。您是否收到任何其他错误,例如包含“Player.h”失败?Player.h在正在编译的源文件的include路径中吗?@BasileStarynkevitch我试过了,但我不明白这有什么意义……我不这么认为。一旦你做了一个typedef,这个名字就不能再使用了,比如,你可以做struct foo foo
,但不能做typedef something foo;富富,代码>,即它污染了名称空间。它并没有增加太多,它增加了很多可读性。例如,看看Gtk在做什么。但是为什么我不能使用typedef??为什么它不起作用?-1因为这不能解决任何问题,它甚至与错误无关。使用typedef。
/* Game.h */
#ifndef GAME_H_
#define GAME_H_
#include "Player.h"
//#include "Card.h"
#define PLAYERSLEN 2 // number of players
#define GAMESLEN 10 //number of games
#define CARDSLEN 64 //cards in one game
typedef struct game{
player_struct *players[PLAYERSLEN];//here is the problem
//card_struct *cards[CARDSLEN]; //her will be same problem
int active;
int playerOnTurn;
char *gameName;
int gameId;
}games_struct;
//typedef struct game game_struct;
//extern games_struct *games_array[GAMESLEN];
void init_game(games_struct *game);
void run_game(games_struct *game);
void *end_game(games_struct *game);
#endif /* GAME_H_ */