从另一个头文件中的一个头文件构造 [英] Struct from one header file in another header file

查看:179
本文介绍了从另一个头文件中的一个头文件构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要花很长时间才能看到这个问题。

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

让我们有头文件Player.h

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

让我们有第二个头文件Game.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_ */

我的typedef的目的是有一个类型player_struct使用这样的东西:

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

这是源文件Player.c - 这是有效的。

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

#include "Player.h"

player_struct *create_player
{
    player_struct *player;

    //body malloc ....

    return player;
}

我使用eclipse编辑器进行C和C ++,我用Linux编译了C项目GCC。

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

为什么我不能在头文件game.h中使用类型player_struct,就像在Player.C中一样,即使我已经包含了头文件Player。 h?

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?

在头文件game.h中的位置进行了注释//这里我想要使用

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

player_struct *players[NUMBEROFPLAYERS];

但它写道无法解析dame类型..

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


类型'player_struct'无法解析

Type 'player_struct' could not be resolved

我尝试了很多方法来写struct和typedef,但没有任何帮助。
我真的不赞成任何想法吗?
谢谢。

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

编辑:
我被要求将所有代码放在那里:
问题在标题Game.h中我使用的类型为player_struct(它的第一行)。

first

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


推荐答案

尝试在不使用typedef的情况下定义它们。

Try to define them without using typedef.

这篇关于从另一个头文件中的一个头文件构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆