结构问题,将它们放在哪里以及如何在标题中引用它们?

发布于 2024-09-07 22:32:33 字数 1455 浏览 4 评论 0原文

好吧,我现在陷入了困境,我对 C 的了解(无论如何都不是最大的)和全能的 Google 似乎都无法帮助我解决这个问题:

我有一些游戏原型的结构:

  • Map(呃...地图..)
  • Chara(敌人玩家的基地)
  • Player(玩家)

现在的问题是:Map 需要对其上的 Player 进行引用,正在构造 Player,为其提供一个 Map 和一个CharaChara 也需要 Map

如果我在头文件中声明结构并用 #ifndef 包装它们,那么当我包含其他标头中的标头时,我会遇到循环依赖循环。

当我在 .c 文件中声明结构时,我使用 extern struct Map map 等,但随后遇到了诸如 无效使用不完整声明forward 的问题结构 XXX 的声明

现在是凌晨 4 点,我想花更多的时间重写引擎的东西(Python 和 JavaScript 中都已经存在了......是的,我有太多时间了!),而不是尝试一切可行的方法。今晚剩余时间的搜索词组合。

我知道这可能是一件非常简单的事情,但这里有 30°C,所以请怜悯我的 C“技能”^^

编辑
由于我的问题使用了 typedef,而 caf 的答案不包括它们,所以我不得不多花点功夫才能使其全部正常工作。因此,为了帮助那些可能通过 SE 找到这个答案的人,我将添加以下代码:

ma​​p.h

typedef struct _Player Player;

typedef struct _Map {
    ...
    Player *player;
    ...
} Map;

ma​​p.c

// include both player.h and chara.h

player.h

typedef struct _Map Map;
typedef struct _Chara Chara;

typedef struct _Player {
    Chara *chara;
    ...
} Player;

Player *player_create(Map *map, int x, int y);

玩家.c

// include player.h, chara.h and map.h

OK, I'm in a dilemma right now, and neither my knowledge of C (which isn't the biggest one anyways) nor the almighty Google seem to be able to help me with this:

I have some structures for a game prototype:

  • Map (Uhhm... the Map..)
  • Chara (A base for Enemies the Players)
  • Player (The Player)

Now the problem is: Map needs a reference to the Player that's on it, Player is being constructed giving it a Map and a Chara, and Chara needs the Map too.

If I declare the structs in the header files and wrap them with #ifndef I run into a cyclic dependency loop when I include headers from within other headers.

When I declare the structs in the .c files, I use extern struct Map map etc. but then I run into problem like invalid use of incomplete declaration or forward declaration of struct XXX.

It's 4 o'clock in the morning here and I would like to spend my time more on rewriting the engine stuff (which already exists both in Python AND JavaScript...yes I've got too much time!) rather then trying every feasible combination of search terms for the rest of the night.

I'm aware that this might be a REALLY easy thing, but it has 30°C in here, so please have mercy with my C "skills" ^^

EDIT
Since my problem used typedefs and caf's answer didn't include them, I had to fiddle a bit more with it in order to get it all working. So to help the people which might find this answer via a SE I'll add the code below:

map.h

typedef struct _Player Player;

typedef struct _Map {
    ...
    Player *player;
    ...
} Map;

map.c

// include both player.h and chara.h

player.h

typedef struct _Map Map;
typedef struct _Chara Chara;

typedef struct _Player {
    Chara *chara;
    ...
} Player;

Player *player_create(Map *map, int x, int y);

player.c

// include player.h, chara.h and map.h

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十雾 2024-09-14 22:32:33

如果您的结构仅包含指向其他结构的指针,则此方法有效:

ma​​p.h

#ifndef _MAP_H
#define _MAP_H

struct player;

struct map {
    struct player *player;
    ...
};

#endif /* _MAP_H */

chara.h

#ifndef _CHARA_H
#define _CHARA_H

struct map;

struct chara {
    struct map *map;
    ...
};

#endif /* _CHARA_H */

player.h

#ifndef _PLAYER_H
#define _PLAYER_H

struct map;
struct chara;

struct player {
    struct map *map;
    struct chara *chara;
    ...
};

#endif /* _PLAYER_H */

如果您的结构之一包含实际的其他结构(包括数组)的实例,那么该结构也需要#include另一个标头。例如,如果 map 包含玩家数组:

ma​​p.h

#ifndef _MAP_H
#define _MAP_H

#include "player.h"

struct map {
    struct player p[10];
    ...
};

#endif /* _MAP_H */

不过,您必须小心循环包含。如果您将 map.h 包含在 player.h 中,则无法在 map 之前将 player.h 包含在另一个源文件中.h - 所以你不会这样做。

This works if your structures only contain pointers to the other structures:

map.h

#ifndef _MAP_H
#define _MAP_H

struct player;

struct map {
    struct player *player;
    ...
};

#endif /* _MAP_H */

chara.h

#ifndef _CHARA_H
#define _CHARA_H

struct map;

struct chara {
    struct map *map;
    ...
};

#endif /* _CHARA_H */

player.h

#ifndef _PLAYER_H
#define _PLAYER_H

struct map;
struct chara;

struct player {
    struct map *map;
    struct chara *chara;
    ...
};

#endif /* _PLAYER_H */

If one of your structures contain actual instances of the other structures (including arrays), then that one would need to #include the other header too. Eg if map contains an array of players:

map.h

#ifndef _MAP_H
#define _MAP_H

#include "player.h"

struct map {
    struct player p[10];
    ...
};

#endif /* _MAP_H */

You do have to be careful about circular includes though. If you included map.h in player.h, then you couldn't include player.h in another source file before map.h - so you wouldn't do that.

梦忆晨望 2024-09-14 22:32:33

确保您的引用是指针而不是对象的实际副本,并且您应该能够很好地前向声明它们。

Make sure your references are pointers rather than actual copies of the objects, and you should be able to forward-declare them just fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文