问题描述
我有一个头文件port.h,port.c,和我的main.c
I have a header file port.h, port.c, and my main.c
我得到以下错误:端口使用了未定义的结构'port_t
I get the following error: 'ports' uses undefined struct 'port_t'
我想我已宣布在我.h文件中的结构并具有.c文件的实际结构是确定的。
I thought as I have declared the struct in my .h file and having the actual structure in the .c file was ok.
我需要有正向的声明,我想要隐藏在我port.c文件中的一些数据。
I need to have the forward declaration as I want to hide some data in my port.c file.
在我port.h我有以下几点:
In my port.h I have the following:
/* port.h */
struct port_t;
port.c:
port.c:
/* port.c */
#include "port.h"
struct port_t
{
unsigned int port_id;
char name;
};
main.c中:
main.c:
/* main.c */
#include <stdio.h>
#include "port.h"
int main(void)
{
struct port_t ports;
return 0;
}
非常感谢您的任何建议,
Many thanks for any suggestions,
推荐答案
不幸的是,编译器需要知道 port_t
(字节),而编译main.c的大小,所以你需要完整的类型定义的头文件。
Unfortunately, the compiler needs to know the size of port_t
(in bytes) while compiling main.c, so you need the full type definition in the header file.
这篇关于未定义的C结构正向声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!