简体   繁体   中英

Error: incomplete type is not allowed

in .h :

typedef struct token_t TOKEN;

in .c :

#include "token.h"

struct token_t
{
    char* start;
    int length;
    int type;
};

in main.c :

#include "token.h"

int main ()
{
    TOKEN* tokens; // here: ok
    TOKEN token;   // here: Error: incomplete type is not allowed
    // ...
}

The error I get in that last line:

Error: incomplete type is not allowed

What's wrong?

In main module there is no definition of the structure. You have to include it in the header, The compiler does not know how much memory to allocate for this definition

TOKEN token;

because the size of the structure is unknown. A type with unknown size is an incomplete type.

For example you could write in the header

typedef struct token_t
{
    char* start;
    int length;
    int type;
} TOKEN;

You need to move the definition of the struct into the header file:

/* token.h */

struct token_t
{
    char* start;
    int length;
    int type;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM