c - 单个 C 头文件中的循环依赖。需要前向声明吗?
问题描述
#ifndef STAGE_TABLE_DEFINITION_HEADER
#define STAGE_TABLE_DEFINITION_HEADER
typedef stage_table_context_t* (*stage_table_function_t)(stage_table_context_t*);
typedef struct {
const char* stage_name;
stage_table_function_t* function;
} stage_t;
typedef struct {
uint32_t error_number;
stage_t* current_stage;
} stage_table_context_t;
#endif
上出现未知类型错误stage_table_context_t
。
函数指针stage_table_function_t
指代stage_table_context_t
和stage_table_context_t
指代stage_table_function_t
。
显然定位在这里并不重要,因为任何一个方向都会导致问题。似乎我需要转发声明舞台表上下文结构,但不确定如何使用 typedef 来执行此操作。
为这个愚蠢的问题道歉,我已经离开 C 6 个月了,我有点脑子放屁。
编辑:修正了代码中的一些错字。
解决方案
你只需要告诉编译器stage_table_context_t
应该是struct
; 这样,您就隐含地向前声明了struct stage_table_context_t
,实际的定义可能会在以后出现。请注意, typedef 没有定义 a struct
,它只是引入了一个别名。所以实际的定义是struct stage_table_context_t { ...
,不管你是否为它引入别名(也不管你为别名使用哪个名字)。
typedef struct stage_table_context_t* (*stage_table_function_t)(struct stage_table_context_t*);
typedef struct {
const char* stage_name;
stage_table_function_t* function;
} stage_t;
struct stage_table_context_t {
uint32_t error_number;
stage_t* current_stage;
};
// optional (if you want to use "stage_table_context_t" as an alias for "struct stage_table_context_t"):
typedef struct stage_table_context_t stage_table_context_t;
推荐阅读
- javascript - 如何使用 Javascript 向移动用户显示消息以请使用更大的屏幕?
- java - 为什么 == 在 Java 中的优先级高于后缀 ++?
- sql - 事务在 Microsoft SQL Server 中究竟是如何工作的
- sockets - 尝试减少 ESP32 上的 TCP 套接字延迟
- c++ - 越界异常,因为 for 循环不会移动到下一个循环/索引
- c# - 将 Windows 音量设置为 0 到 100
- events - 事件未到达 F#
- sql - 迭代列中的时间戳行值并在 SQL 中执行 countif?
- typescript - Typescript 使用与输入 arg 不同的键和值来减少 acc
- python - 检查 Selenium 元素是否包含一个类中的文本,然后打印另一类的文本