gpt4 book ai didi

c - 未定义的 C 结构前向声明

转载 作者:太空狗 更新时间:2023-10-29 16:28:19 25 4
gpt4 key购买 nike

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

我收到以下错误:'ports' 使用未定义的结构 'port_t'

我认为我已经在我的 .h 文件中声明了结构并且在 .c 文件中具有实际结构是可以的。

我需要前向声明,因为我想在我的 port.c 文件中隐藏一些数据。

在我的 port.h 中,我有以下内容:

/* port.h */
struct port_t;

端口.c:

/* port.c */
#include "port.h"
struct port_t
{
unsigned int port_id;
char name;
};

主.c:

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

int main(void)
{
struct port_t ports;

return 0;
}

非常感谢您的任何建议,

最佳答案

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

关于c - 未定义的 C 结构前向声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/621356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com