0

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_ */
user1097772
  • 3,499
  • 15
  • 59
  • 95
  • 1
    Look at the *preprocessed form* of your `*.c` code file. On Linux, you'll get them with `gcc -C -E Player.c > Player.i` then start a pager or an editor on them, e.g. `less Player.i` or `emacs Player.i` – Basile Starynkevitch Jan 26 '12 at 14:49
  • By the way, your function `create_player` doesn't actually create a player. It returns an invalid pointer. – Mr Lister Jan 26 '12 at 14:49
  • Check this question out http://stackoverflow.com/questions/228684/how-to-declare-a-structure-in-a-header-that-is-to-be-used-by-multiple-files-in-c – Carl Winder Jan 26 '12 at 14:50
  • Are you getting any other errors - like including "Player.h" failed? Is Player.h in the include path of the source file being compiled? – Amarghosh Jan 26 '12 at 14:50
  • @BasileStarynkevitch I tryed it but i did't see what is point of this ... – user1097772 Jan 26 '12 at 15:07
  • @MrLister I 've edited the function ... there will be also body of the function before return .. malloc of memory and so on .. my problem is using the struct – user1097772 Jan 26 '12 at 15:08
  • Show us the *exact* definition of `game_struct` that triggers the error message. Based on what you have here and what you're describing , I'm betting that you have a typo somewhere in the `game_struct` definition. – John Bode Jan 26 '12 at 20:21
  • @Here you are, at the bottom of my question there are no both header files. games_struct is in Game.h – user1097772 Jan 26 '12 at 21:04
  • The problem is not in the code you have posted, it is all valid, correct C. Compiles just fine in GCC. – Lundin Jan 27 '12 at 07:34
  • I now it now too. Problem was in eclipse - just dump environment .. since I added the source code where I include both headers and use that struct where was problem and than Eclipse started to understand it. And stoped report errors. This strange behaviour of editors wiil kill me some day .. – user1097772 Jan 27 '12 at 09:13

1 Answers1

0

Try to define them without using typedef.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150