本文介绍了未定义的 C 结构前向声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个头文件 port.h、port.c 和我的 main.c

I have a header file port.h, port.c, and my main.c

我收到以下错误:ports"使用未定义的结构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 */
#include "port.h"
struct port_t
{
    unsigned int port_id;
    char name;
};

main.c:

/* main.c */
#include <stdio.h>
#include "port.h"

int main(void)
{
struct port_t ports;

return 0;
}

非常感谢您的任何建议,

Many thanks for any suggestions,

推荐答案

不幸的是,编译器在编译 main.c 时需要知道 port_t 的大小(以字节为单位),因此您需要完整的头文件中的类型定义.

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 结构前向声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 00:59
查看更多