Invalid application of sizeof to incomplete type with a struct

前端 未结 5 1770
你的背包
你的背包 2020-12-06 04:06

I have a struct where I put all the information about the players. That\'s my struct:

struct player{
   int startingCapital;
   int currentCapital;
   int s         


        
5条回答
  •  盖世英雄少女心
    2020-12-06 04:47

    It means the file containing main doesn't have access to the player structure definition (i.e. doesn't know what it looks like).

    Try including it in header.h or make a constructor-like function that allocates it if it's to be an opaque object.

    EDIT

    If your goal is to hide the implementation of the structure, do this in a C file that has access to the struct:

    struct player *
    init_player(...)
    {
        struct player *p = calloc(1, sizeof *p);
    
        /* ... */
        return p;
    }
    

    However if the implementation shouldn't be hidden - i.e. main should legally say p->canPlay = 1 it would be better to put the definition of the structure in header.h.

提交回复
热议问题