我有一个结构:
struct room;
...
/*some more structs */
typedef struct {
int state;
int id;
struct room* north;
struct room* south;
struct room* east;
struct room* west;
creature* creatures[10];
} room;
打印内容的函数:
void printContentsOfRoom(room* r) {
printf("\nRoom %d:\n", r->id);
printf("\tState: %s\n", getState(r));
printf("\n\tNeighbors:\n");
if (r->north.id != -1)
printf("\t North: Room %d\n",((room*)r->north)->id);
if (r->east.id != -1)
printf("\t East: Room %d\n",((room*)r->east)->id);
if (r->south.id != -1)
printf("\t South: Room %d\n",((room*)r->south)->id);
if (r->west.id != -1)
printf("\t West: Room %d\n",((room*)r->west)->id);
printf("\n\tCreatures:\n");
for (int i = 0; i < 10; i++) {
creature* c = (creature*)r->creatures[i];
if (c) {
if (c == pc)
printf("\t %s\n",getType(c));
else
printf("\t %s %d\n",getType(c),c->id);
}
}
}
我试图这样做,以便如果房间ID设置为-1(在程序运行时设置),它不会打印有关该房间的信息。在编译时,我收到错误&#34;请求成员&#39; id&#39;在某种结构或联合的东西中。&#34;
此外,我尝试设置if条件r-&gt; north-&gt; id,并返回错误&#34;解除引用指向不完整类型&#39; struct room&#39;&#34; < / p>
答案 0 :(得分:1)
我认为你想做这样的前瞻声明:
typedef struct room room;
struct room{
.
.
.
};
然后您可以使用r->north->id
。请参阅此问题:How to define a typedef struct containing pointers to itself?