私はこれを本当にかなり長い時間動かしていますが、どこがポブレムであるかはまだ分かりません。別のヘッダーファイルの1つのヘッダーファイルからの構造体
のは
#ifndef PLAYER_H_
#define PLAYER_H_
typedef struct player
{
char *name;
int gameId;
int socketfd;
int points;
int state;
}player_struct;
#endif /* PLAYER_H_ */
ヘッダファイル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;
}
私はCとC++用のEclipseエディタを使用していますが、私はLinux GCCでコンパイルされたCプロジェクトを持っています。
ヘッダーファイルgame.hでPlayer.Cと同じ方法で、Player.hというヘッダーファイルをインクルードしても使用できないのはなぜですか?ヘッダファイルgame.h内の場所で
はできなかった
タイプ 'player_struct' ..私は
player_struct *players[NUMBEROFPLAYERS];
を使いたいが、それは貴婦人タイプは解決できなかったと書いているHERE // comented解決する
私は構造体とtypedefを書くために多くの方法を試しましたが、何も役立たなかった。 私は本当にそれを理解していないのですか?おかげさまで
EDITED: 私はplayer_struct(最初の行)を使用すると、問題がヘッダGame.hにあります。
最初
/* Player.h */
#ifndef PLAYER_H_
#define PLAYER_H_
typedef struct player{
char *name; // player name
int gameId; // what game
int points; //how money points
int socketfd; //socket descriptor of player
int state;
} player_struct;
//create player
player_struct *create_player();
//delete player
void delete_player(player_struct *player);
#endif /* PLAYER_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_ */
'* .c'コードファイルの*前処理された形式*を見てください。 Linuxでは、 'gcc -C -E Player.c> Player.i'でそれらを手に入れ、ポケベルやエディタを起動します。 'less Player.i'または' emacs Player.i' –
ところで、あなたの関数 'create_player'は実際にプレーヤーを作成しません。無効なポインタを返します。 –
この質問をチェックアウトhttp://stackoverflow.com/questions/228684/how-to-declare-a-structure-in-a-header-that-is-to-be-used-by-multiple-files-in -c –