结构问题,将它们放在哪里以及如何在标题中引用它们?
好吧,我现在陷入了困境,我对 C 的了解(无论如何都不是最大的)和全能的 Google 似乎都无法帮助我解决这个问题:
我有一些游戏原型的结构:
Map
(呃...地图..)Chara
(敌人玩家的基地)Player
(玩家)
现在的问题是:Map
需要对其上的 Player
进行引用,正在构造 Player
,为其提供一个 Map
和一个Chara
和 Chara
也需要 Map
。
如果我在头文件中声明结构并用 #ifndef
包装它们,那么当我包含其他标头中的标头时,我会遇到循环依赖循环。
当我在 .c 文件中声明结构时,我使用 extern struct Map map
等,但随后遇到了诸如 无效使用不完整声明
或 forward 的问题结构 XXX 的声明
。
现在是凌晨 4 点,我想花更多的时间重写引擎的东西(Python 和 JavaScript 中都已经存在了......是的,我有太多时间了!),而不是尝试一切可行的方法。今晚剩余时间的搜索词组合。
我知道这可能是一件非常简单的事情,但这里有 30°C,所以请怜悯我的 C“技能”^^
编辑
由于我的问题使用了 typedef,而 caf 的答案不包括它们,所以我不得不多花点功夫才能使其全部正常工作。因此,为了帮助那些可能通过 SE 找到这个答案的人,我将添加以下代码:
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);
玩家.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的结构仅包含指向其他结构的指针,则此方法有效:
map.h
chara.h
player.h
如果您的结构之一包含实际的其他结构(包括数组)的实例,那么该结构也需要
#include
另一个标头。例如,如果map
包含玩家数组:map.h
不过,您必须小心循环包含。如果您将
map.h
包含在player.h
中,则无法在map 之前将
- 所以你不会这样做。player.h
包含在另一个源文件中.hThis works if your structures only contain pointers to the other structures:
map.h
chara.h
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 ifmap
contains an array of players:map.h
You do have to be careful about circular includes though. If you included
map.h
inplayer.h
, then you couldn't includeplayer.h
in another source file beforemap.h
- so you wouldn't do that.确保您的引用是指针而不是对象的实际副本,并且您应该能够很好地前向声明它们。
Make sure your references are pointers rather than actual copies of the objects, and you should be able to forward-declare them just fine.