Lorenzo G.
Lorenzo G.

Reputation: 21

Error: field has incomplete type in C

Here's a structure definition in my library (mylib.h):

struct Terra {
enum Tipo_terra tipo_terra;
enum Tipo_mostro tipo_mostro;
short tesoro;
struct Terra* terra_successiva;
};

Here's the error:

In file included from progexam.c:3:0:    
mylib.h:20:18: error: field ‘tipo_terra’ has incomplete type    
mylib.h:21:19: error: field ‘tipo_mostro’ has incomplete type

I have used typedef to define 'enum tipo_terra' and 'enum tipo_mostro' just like this:

typedef enum { deserto, foresta, palude, villaggio, pianura } tipo_terra;
typedef enum { nessuno, scheletro, lupo, orco, drago } tipo_mostro;

And yes, I have (of course) included my library using

#include "mylib.h"

I have tried to move typedef before and after the structure declaration, no changes, same error. Can you help me, please? Thanks!

Upvotes: 0

Views: 3006

Answers (2)

Ian Abbott
Ian Abbott

Reputation: 17403

Here's an alternative answer using enum tags instead of typedef:

enum Tipo_terra { deserto, foresta, palude, villaggio, pianura };
enum Tipo_mostro { nessuno, scheletro, lupo, orco, drago };

struct Terra {
    enum Tipo_terra terra;
    enum Tipo_mostro mostro;
    short tesoro;
    struct Terra* terra_successiva;
};

Another possibility is to use both an enum tag and a typedef as follows:

typedef enum Tipo_terra { deserto, foresta, palude, villaggio, pianura } Tipo_terra;
typedef enum Tipo_mostro { nessuno, scheletro, lupo, orco, drago } Tipo_Mostro;

Then you could use either enum Tippo_terra or Tippo_terra in subsequent code, as they are both the same type.

Note that typedef does not create a type, it only adds a name to a pre-existing type, or to a newly defined (and possibly anonymous) type.

Upvotes: 1

LPs
LPs

Reputation: 16213

Soome problems:

typedef enum { deserto, foresta, palude, villaggio, pianura } tipo_terra;
typedef enum { nessuno, scheletro, lupo, orco, drago } tipo_mostro;

struct Terra {
    tipo_terra terra;
    tipo_mostro mostro;
    short tesoro;
    struct Terra* terra_successiva;
};
  1. You typedefed your enum so the type must be used directly, no enum prefix.
  2. is case sensistive, so Tipo_mostro is different then tipo_mostro.

Upvotes: 3

Related Questions