error: invalid use of Undefined type 'struct Fila'

Asked

Viewed 397 times

0

I’m implementing a queue to control the threads created, in C. So as the thread is not the first in line she waits to be finished.

example:

//
//  Enquanto não é primeira da fila
//
while(idd   !=  PPFila.dados[PPFila.primeiro])
{

    //
    //  Aguarda
    //
    GEDI_CLOCK_Delay(50);
}

and on that line while(idd != PPFila.dados[PPFila.primeiro])shows the following error: error: error: invalid use of undefined type 'struct Fila.

Excerpts:

Struct:

struct  Fila
{

    int         capacidade;
    int         *dados;
    int         primeiro;
    int         ultimo;
    int         nItens;

};

Function:

void            funcao
            (
                char    *as_comando_buffer,
                int     an_codigo_retorno,
                char    *as_output,
                int     an_output_lenght,
                int     idd
            )

BS, I’m using them from another file with extern

extern  struct  Fila
PPFila,
AUTFila;

Does anyone have any tips how I can fix this?

  • Show the statement of idd.

  • ready, added

  • You’re probably missing one #include in the file where you have the function with the while cycle. I assume that there is no definition of struct Fila active and compiler complains.

1 answer

2


extern indicates functions or variables that are in other files - but gives no hint to the compiler of which is the signature of a function, or, in the case of structs, its size and its actual structure.

In order to use a struct, each file . C when compiled has to "see" the struct statement. This is done by placing the struct declaration in a header file ( .h ) - and including that file .h in all files .c that you will compile.

In short: put your struct statement in a file. h separate (for example, "row.h"), and both in the file where you create the global structures, and in the file where your error occurred, use the directive #include "fila.h".

What I see in large and recent projects is also the custom of always creating a typedef with a struct - in order to dispense with the use of the word struct in statements of such structures (and perhaps some more advantages for the compiler and adjacent tools to be able to optimize your code). Then , the file ". h" would contain:

typedef struct _Fila

{

    int         capacidade;
    int         *dados;
    int         primeiro;
    int         ultimo;
    int         nItens;

} Fila;

And then to declare Ppfila and Autfila (in file . c)

Fila PPFila, AUTFila;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.