Struct from one header file in another header file

北城以北 提交于 2019-12-25 08:26:44

问题


I'm going trought this really quite long time and still don't see where could be the poblem.

Let's have header file 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_ */

And let's have second header file Game.h

#ifndef GAME_H_
#define GAME_H_

#include "Player.h"

#define NUMBEROFPLAYERS 2

typedef struct game
{

//Here

}game_struct;

#endif /* GAME_H_ */

The purpose of my typedef is to have a type player_struct using something like this:

It's source file Player.c - this works.

#include "Player.h"

player_struct *create_player
{
    player_struct *player;

    //body malloc ....

    return player;
}

I use eclipse editor for C and C++, I have C project compiled with Linux GCC.

Why I'm not able to use type player_struct in header file game.h same way as in Player.C even if I have included the header file Player.h?

at place in header file game.h comented //HERE I want use

player_struct *players[NUMBEROFPLAYERS];

but it writes that dame type could not be resolved ..

Type 'player_struct' could not be resolved

I tryed many ways to write the struct and typedef but nothing helped. I really don't unerstand it any ideas? Thanks.

EDITED: I was asked to put there all my code so: problem is in header Game.h when i use type player_struct (it's first row).
first

 /* 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_ */

second

  /* 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_ */

回答1:


Try to define them without using typedef.



来源:https://stackoverflow.com/questions/9019841/struct-from-one-header-file-in-another-header-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!