C Typedef - Incomplete Type

后端 未结 4 1160
醉梦人生
醉梦人生 2020-12-03 09:00

So, out of the blue, the compiler decides to spit this in face: \"field customer has incomplete type\".

Here\'s the relevant snippets of code:

customer.c

相关标签:
4条回答
  • 2020-12-03 09:24

    What you have is a forward declaration of Customer structure that you are trying to instantiate. This is not really allowed because compiler has no idea about the structure layout unless it sees it definition. So what you have to do is move your definition from the source file into a header.

    0 讨论(0)
  • 2020-12-03 09:26

    It seems that something like

    typedef struct foo bar;
    

    won't work without the definition in the header. But something like

    typedef struct foo *baz;
    

    will work, as long as you don't need to use baz->xxx in the header.

    0 讨论(0)
  • 2020-12-03 09:30

    In C, the compiler needs to be able to figure out the size of any object that is referenced directly. The only way that the sizeof(CustomerNode) can be computed is for the definition of Customer to be available to the compiler when it is building customer_list.c.

    The solution is to move the definition of the struct from customer.c to customer.h.

    0 讨论(0)
  • 2020-12-03 09:33

    Move the struct declaration to the header:

    customer.h
    typedef struct CustomerStruct
    {
    ...
    }
    
    0 讨论(0)
提交回复
热议问题