如何在 .h 中转发 typedef 结构
我有 Preprocessor.h
#define MAX_FILES 15
struct Preprocessor {
FILE fileVector[MAX_FILES];
int currentFile;
};
typedef struct Preprocessor Prepro;
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
然后我意识到我必须将声明与定义分开。所以我创建了 Preprocessor.c:
#define MAX_FILES 15
struct Preprocessor {
FILE fileVector[MAX_FILES];
int currentFile;
};
typedef struct Preprocessor Prepro;
现在 Preprocessor.h 是:
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
显然,这不起作用,因为 Pr..h 不知道 Prepro 类型。我已经尝试了几种组合,但没有一个有效。我找不到解决方案。
I have
Preprocessor.h
#define MAX_FILES 15
struct Preprocessor {
FILE fileVector[MAX_FILES];
int currentFile;
};
typedef struct Preprocessor Prepro;
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
I realized then that I had to separate declarations from definitions. So I created Preprocessor.c:
#define MAX_FILES 15
struct Preprocessor {
FILE fileVector[MAX_FILES];
int currentFile;
};
typedef struct Preprocessor Prepro;
And Preprocessor.h is now:
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
That obviously, doesn't work because Pr..h doesn't know Prepro type. I already tried several combinations, none of them worked. I can't find the solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
将
typedef struct Preprocessor Prepro;
移至该文件的标头以及 c 文件中的定义以及 Prepro_init 定义。这将毫无问题地为您转发声明。预处理器.h
预处理器.c
Move the
typedef struct Preprocessor Prepro;
to the header the file and the definition in the c file along with the Prepro_init definition. This is will forward declare it for you with no issues.Preprocessor.h
Preprocessor.c
如果您想隐藏
Preprocessor
的定义,只需将其放入头文件中即可:但更一般而言,您可能还需要标头中的
Preprocessor
定义文件,以允许其他代码实际使用它。If you want to hide the definition of
Preprocessor
, you can simply put this in the header file :But more generally, you'll probably also need the
Preprocessor
definition in the header file, to allow other code to actually use it.您已将应在
.h
中的内容放入.c
中,反之亦然。Prepro_init
必须位于.c
文件中,并且该文件必须#include "Preprocessor.h"
。You have put in
.c
what should be in.h
, and vice versa.Prepro_init
must be in.c
file, and that file must#include "Preprocessor.h"
.YAS:另一个解决方案。
Preprocessor.h
Preprocessor.c
现在它可以工作了。我想这就是你的要求。希望有帮助。
缺点:
Preprocessor 结构的成员必须是预先确定的。即头文件使用成员currentFile。因此,包含 Preprocessor.h 的 c 文件必须具有类型定义为 Prepro 的结构,并且该结构必须包含成员 currentFile。(在本例中)。
一年前,我在编写头文件以图形树格式显示用户 Avl 树时遇到了同样的问题。
YAS:Yet Another Solution.
Preprocessor.h
Preprocessor.c
Now it will work. I think this is your requirement. Hope it helps.
Drawback:
The members of the structure Preprocessor, must be predetermined. i.e the header file uses the member currentFile. So, c file which includes Preprocessor.h must have a structure which is typedefined as Prepro and that structure must include a member currentFile.(in this case).
The same problem I had a year before, while writting a header file to display a users Avl tree in a graphical tree format.
我建议您遵循 Linus[1],并且不要使用 typedef struct。
如果您可以控制库:
包含文件
在实现文件中
如果您无法控制库:
要求维护者从包含文件中删除这些污染性的 typedef。
摘要
不要使用 typedef struct。
[1] http://yarchive.net/comp/linux/typedefs.html
I would suggest that you follow Linus[1], and do not use typedef struct.
If you are in control over the library:
Include file
In implementation file
If you are not in control over the library:
Ask the maintainer to remove these polluting typedefs from the include files.
Summary
Do not use typedef struct.
[1] http://yarchive.net/comp/linux/typedefs.html
交换
.h
和.c
文件。在.c
中包含标头。另请参阅有关声明、定义和头文件用途的书籍。
Swap
.h
and.c
file. Include header in.c
.Also refer to a book about declarations, definitions and what header files do.