【发布时间】:2016-10-09 02:40:20
【问题描述】:
我试图弄清楚前向声明究竟是如何相互作用的。当前向声明一个采用 typedef 结构的函数时,有没有办法让编译器接受以前前向声明(但未实际定义)的结构作为参数?
我正在工作的代码:
typedef struct{
int year;
char make[STR_SIZE];
char model[STR_SIZE];
char color[STR_SIZE];
float engineSize;
}automobileType;
void printCarDeets(automobileType *);
我希望我能做什么:
struct automobileType;
void printCarDeets(automobileType *);
//Defining both the struct (with typedef) and the function later
我觉得我要么遗漏了一些非常基本的东西,要么不理解编译器如何处理结构的前向声明。
【问题讨论】:
-
我意识到这是一个老问题,但没有人提到这是 C 和 C++ 之间的区别之一。在 C 中,你可以有一个 typedef、一个结构、一个联合和一个同名的枚举 - 在 C++ 中你只能有一个,因为它处理标记名就像 C 处理 typedef。
标签: c struct typedef forward-declaration