1

I have these types:

struct netlib_nw {
  int id;
  union netlib_op op;
};

union netlib_op {
  struct netlib_op_readdir op_readdir;
  struct netlib_op_readdir_recv op_readdir_recv;
};

struct netlib_op_readdir {
  char* path;
};

struct netlib_op_readdir_recv {
  char** recv;
};

I have them cause I wanna tranfer the netlib_nw via network with different stuff in it.

Here is the error I get:

error: field has incomplete type 'union netlib_op'
  union netlib_op op;
                  ^
note: forward declaration of 'union netlib_op'
  union netlib_op op;

How to solve that? I do not understand what's wrong with that?

Thanks for your help!

Martin Fischer
  • 469
  • 1
  • 7
  • 21
  • Just like you do, the compiler reads source files from the top to the bottom. Just as you, it will get confused if it encounters a variable declaration of a type which has not been previously defined anywhere. – Lundin Jun 18 '19 at 14:53

1 Answers1

3

Just rearrange the definitions:

struct netlib_op_readdir {
    char* path;
};

struct netlib_op_readdir_recv {
    char** recv;
};

union netlib_op {
    struct netlib_op_readdir op_readdir;
    struct netlib_op_readdir_recv op_readdir_recv;
};

struct netlib_nw {
    int id;
    union netlib_op op;
};

The issue is that in the original code, while parsing netlib_nw sees a declaration of type netlib_op but that type is not defined (thus it is incomplete). If for example you had used a member which is a pointer or reference to netlib_nw then everything would be fine since you can use them with a forward declared class.

So by rearranging the structures so that they are defined in order of dependencies you get rid of the error.

Rudolfs Bundulis
  • 11,636
  • 6
  • 33
  • 71