gpt4 book ai didi

c - 头文件中的结构

转载 作者:太空宇宙 更新时间:2023-11-04 07:02:08 28 4
gpt4 key购买 nike

我想要两个共享结构。

在我的 .h 中:

typedef struct {
char nombre[25];
int pv;
char ataque1[25];
char ataque2[25];
char ataque3[25];
char ataque4[25];
char ataque1_estado[25];
char ataque2_estado[25];
char ataque3_estado[25];
char ataque4_estado[25];
char estado[25];
int pd_max[4];
int pd_min[4];
int pp[4];
int pociones;
int antidotos;
} pokemon;

extern pokemon pokemon1;
extern pokemon pokemon2;

在我的 .c 文件中:

#include "file.h"
//file body, i use here structs in file.h

但是它不编译。 GCC 显示此错误:

main.c:(.text+0x440): referencia a `pokemon1' sin definir

怎么了?

最佳答案

头文件中的最后两行是pokemon 对象的声明,它允许您从多个.c 文件中使用它们。但是在其中一个 .c 文件中,您必须定义它们,在您的情况下,这与声明相同但没有 extern 关键字:

pokemon pokemon1;
pokemon pokemon2;

关于c - 头文件中的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36749531/

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