C variabile ha inizializzatore incompleto
-
26-09-2019 - |
Domanda
Sto cercando di fare una struct con un valore predefinito, come descritto di seguito: valori di default in un gruppo C Struct . Tuttavia, ho questo codice C, all'interno di un file di intestazione:
/* tokens.h */
typedef struct {
char *ID;
char *KEY;
char *TYPE;
} tokens;
const struct tokens TOKENS_DFLT = {
"id",
"key",
"type"
};
E sto ottenendo un errore sulla linea 7 dicendo:
error: variable 'TOKENS_DFLT' has initializer but incomplete type
Qualcuno può spiegare a me che cosa questo problema e come posso risolvere il problema ed evitare che in futuro?
Soluzione
Non hai definito struct tokens
. Hai definito un struct
senza nome e contemporaneamente typedef
-ed al nome del tipo tokens
.
Se si aveva invece definito
typedef struct tokens_ {
char *ID;
char *KEY;
char *TYPE;
} tokens;
Poi si potrebbe dichiarare la costante o :
const struct tokens_ TOKENS_DFLT = { ... };
o
const tokens TOKENS_DFLT = { ... };
Come è, sei tra i due sgabelli.
Altri suggerimenti
Questa:
const struct tokens TOKENS_DFLT = {
"id",
"key",
"type"
};
dovrebbe essere:
const tokens TOKENS_DFLT = {
"id",
"key",
"type"
};
Dal momento che avete definito il nome tokens
a struct tokens
media.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow