C - error: array type has incomplete element type in a extern struct declaration
C - error: array type has incomplete element type in a extern struct declaration
我有下一个代码:
// FILE: HEADERS.h
extern struct viaje viajes[];
extern struct cliente clientes[];
// FILE: STRUCTS.c
struct viaje {
char identificador[30+1];
char ciudadDestino[30+1];
char hotel[30+1];
int numeroNoches;
char tipoTransporte[30+1];
float precioAlojamiento;
float precioDesplazamiento;
};
struct cliente {
char dni[30+1];
char nombre[30+1];
char apellidos[30+1];
char direccion[30+1];
int totalViajes;
struct viaje viajes[50];
} clientes[20];
当我尝试编译代码时出现下一个错误:error: array type has incomplete element type in a extern struct declaration
我不知道为什么会这样。我试过在 Structs 定义之后也包含 header 并且我没有收到任何错误,但这是错误的,正确的方法是 Define -> Declare,而不是 Declare -> Define.
为什么会这样?谢谢。
如果您定义或声明结构的实例,则需要先定义该结构。否则,编译器无法计算出结构的大小或它的成员是什么。
您需要将结构定义放在头文件中 extern
声明之前的第一个位置:
struct viaje {
char identificador[30+1];
char ciudadDestino[30+1];
char hotel[30+1];
int numeroNoches;
char tipoTransporte[30+1];
float precioAlojamiento;
float precioDesplazamiento;
};
struct cliente {
char dni[30+1];
char nombre[30+1];
char apellidos[30+1];
char direccion[30+1];
int totalViajes;
struct viaje viajes[50];
}; // note that there are no instances defined here
extern struct viaje viajes[];
extern struct cliente clientes[];
那么您的 .c 文件将包含以下实例:
// Changed size viajes from 20 to 50
struct viaje viajes[50];
struct cliente clientes[20];
我有下一个代码:
// FILE: HEADERS.h
extern struct viaje viajes[];
extern struct cliente clientes[];
// FILE: STRUCTS.c
struct viaje {
char identificador[30+1];
char ciudadDestino[30+1];
char hotel[30+1];
int numeroNoches;
char tipoTransporte[30+1];
float precioAlojamiento;
float precioDesplazamiento;
};
struct cliente {
char dni[30+1];
char nombre[30+1];
char apellidos[30+1];
char direccion[30+1];
int totalViajes;
struct viaje viajes[50];
} clientes[20];
当我尝试编译代码时出现下一个错误:error: array type has incomplete element type in a extern struct declaration
我不知道为什么会这样。我试过在 Structs 定义之后也包含 header 并且我没有收到任何错误,但这是错误的,正确的方法是 Define -> Declare,而不是 Declare -> Define.
为什么会这样?谢谢。
如果您定义或声明结构的实例,则需要先定义该结构。否则,编译器无法计算出结构的大小或它的成员是什么。
您需要将结构定义放在头文件中 extern
声明之前的第一个位置:
struct viaje {
char identificador[30+1];
char ciudadDestino[30+1];
char hotel[30+1];
int numeroNoches;
char tipoTransporte[30+1];
float precioAlojamiento;
float precioDesplazamiento;
};
struct cliente {
char dni[30+1];
char nombre[30+1];
char apellidos[30+1];
char direccion[30+1];
int totalViajes;
struct viaje viajes[50];
}; // note that there are no instances defined here
extern struct viaje viajes[];
extern struct cliente clientes[];
那么您的 .c 文件将包含以下实例:
// Changed size viajes from 20 to 50
struct viaje viajes[50];
struct cliente clientes[20];