简体   繁体   中英

C: Incomplete definition of type struct

this error seems very easy to fix but i've been trying and have no clue.

So i have three files:

symtable.h:

typedef struct symbolTable *SymTable_T;

symtablelist.c:

#include "symtable.h"

struct Node{
    char* key;
    void* value;
    struct Node* next;
};

struct symbolTable{
    struct Node* head;
    int length;
};

SymTable_T SymTable_new(void){

/* code */

}

And main.c:

#include "symtable.h"

int main(int argc, const char * argv[]) {
    // insert code here...
    SymTable_T emptyTable = SymTable_new();

    emptyTable->length = 3;   <------- ERROR

    return 0;
}

I'm getting error: Incomplete definition of type "struct symbolTable"

Can anyone please give me a hint?

The reason i declare my struct in my source file is that i will have another implementation for the header file. so is there another way to fix my bug beside moving my struct declaration?

You can't access the members directly with an opaque pointer - if you keep the implementation in a separate source file, you'll have to access all the members via your interface, and not directly mess with the struct.

For instance, add this to symtable.h :

void SymTable_set_length(SymTable_T table, int len);

this to symtablelist.c :

void SymTable_set_length(SymTable_T table, int len)
{
    table->length = len;
}

and in main.c change this:

emptyTable->length = 3;

to this:

SymTable_set_length(emptyTable, 3);

although in this specific case passing the length as an argument to SymTable_new() is an obviously superior solution. Even more superior is not letting the user set the length of a linked list data structure at all - the length is the number of items in it, and it is what it is. It would make no sense to, for instance, add three items to the list, and then allow main.c to set the length to 2 . symtablelist.c can calculate and store the length privately, and main.c can find out what the length is, but it doesn't make much sense for main.c to be able to set the length directly. Indeed, the whole point of hiding the members of a struct behind an opaque pointer like this is precisely to prevent client code from being able to mess with the data like that and breaking the data structure's invariants in this manner.

If you want to access the members directly in main.c , then you have to have the struct definition visible, there is no alternative. This will mean either putting the struct definition in the header file (recommended) or duplicating it in main.c (highly unrecommended).

In typedef symbolTable *SymTable_T; , you refer to a non-existent type symbolTable . In C (unlike C++) the type is named struct symbolTable . (Note: the question has changed to fix this since answering it.)

There's a second problem. In main.c the code will need to be able to see the definition of struct symbolTable for you to be able to refer to fields of emptyTable . At the moment, the definition is hidden in a .c file... it should be moved to the header.

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