【发布时间】:2012-03-15 17:24:05
【问题描述】:
作为一般做法,我正在尝试尽量减少 #include 文件的相互依赖性。
在 xxx.h 我有:
struct my_struct; // partial decl to satisfy use of my_struct*
void funct(struct my_struct* ms); // uses the partial def
如何用 typedef 的结构做类似的部分 decl? 我在第三个 #include 中有一个实际的 decl,看起来像(比如在 yyy.h 中):
typedef struct my_data_s {
int ival;
... struct's other components ...
} my_data_t;
我只想要一个在 xxx.h 中引用 typedef 的有代表性的 decl:
typedef struct my_data_s my_data_t; // actual full decl is elsewhere
void funct2(my_data_t* md);
此尝试导致“重新定义 typedef my_data_t”错误。 (使用 gcc 4.4.3 / Ubuntu 10.4)其他随机搜索尝试(例如,在 typedef 中添加“{}”)也会出错。
我知道编译器只需要知道函数需要一个指针,所以这似乎应该是可能的。到目前为止,没有发现编译时没有错误/警告。
我查看了其他问题和答案,找不到解决的问题。似乎应该有一种众所周知的方法来做到这一点(?!)(我知道我可以在 #include xxx.h 时 #include yyy.y - 试图避免这种依赖关系。)谢谢。
【问题讨论】: