在GBDK C中转发typedef结构的声明

bas*_*aus 4 c struct typedef gameboy gbdk

我正在使用GBDK C为原版Game Boy创建游戏,我遇到了一个小问题.我的游戏中的每个房间都需要有不同portals,但每个portal房间都需要参考一个房间.这是代码的缩减版本:

typedef struct {
    Portal portals[10];
} Room;

typedef struct {
    Room *destinationRoom;
} Portal;
Run Code Online (Sandbox Code Playgroud)

有关如何实现这一目标的任何建议?我尝试struct Portal;在文件顶部添加一个前向声明,但它没有帮助.


使用以下代码:

typedef struct Room Room;
typedef struct Portal Portal;

struct Room {
    Portal portals[10];
};

struct Portal {
    Room *destinationRoom;
};
Run Code Online (Sandbox Code Playgroud)

给我这个错误:

parse error: token -> 'Room' ; column 11
*** Error in `/opt/gbdk/bin/sdcc': munmap_chunk(): invalid pointer: 0xbfe3b651 ***
Run Code Online (Sandbox Code Playgroud)

chq*_*lie 5

重新排序定义并为RoomPortal类型编写前向声明:

typedef struct Room Room;
typedef struct Portal Portal;

struct Portal {
    Room *destinationRoom;
};

struct Room {
    Portal portals[10];
};
Run Code Online (Sandbox Code Playgroud)

请注意,我将typedef Portal实际struct Portal定义与一致性分开,即使它不是绝对必要的.

另请注意,此样式与C++兼容,其中typedef是隐式的,但可以通过这种方式显式编写,或者使用简单的前向声明 struct Room;

如果由于某种原因你不能使用相同的标识符和struct标记typedef,你应该这样声明结构:

typedef struct Room_s Room;
typedef struct Portal_s Portal;

struct Portal_s {
    Room *destinationRoom;
};

struct Room_s {
    Portal portals[10];
};
Run Code Online (Sandbox Code Playgroud)