C 头文件中的结构
我想要两个共享结构 在my.h中:C 头文件中的结构,c,struct,C,Struct,我想要两个共享结构 在my.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]
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;
在my.c文件中:
#include "file.h"
//file body, i use here structs in file.h
但它没有编译。GCC显示此错误:
main.c:(.text+0x440):引用“口袋妖怪”的定义
有什么问题吗?头文件中的最后两行是
口袋妖怪
对象的声明,允许您从多个.c文件中使用它们。但在其中一个.c文件中,您必须定义它们,这与声明相同,但没有extern
关键字:
pokemon pokemon1;
pokemon pokemon2;
头文件中的最后两行是
pokemon
对象的声明,允许您从多个.c文件中使用它们。但在其中一个.c文件中,您必须定义它们,这与声明相同,但没有extern
关键字:
pokemon pokemon1;
pokemon pokemon2;
@是的,但我想在两个.c文件中使用相同的结构,共享结构信息。是的,你会的,别担心。您必须在唯一的.c文件中定义对象,但由于.h文件中的
extern
,其他.c文件将使用相同的对象。@atturri非常感谢!我理解并编译得很好:)@atturri是的,但我想在两个.c文件中使用相同的结构,共享结构信息。是的,你会的,别担心。您必须在唯一的.c文件中定义对象,但由于.h文件中的extern
,其他.c文件将使用相同的对象。@atturri非常感谢!我理解和编译得很好:)