I have a struct that I use in header file1. I now also need that struct in header file2 because it is used in function prototypes. I have included header file1 in header file2, but this give a lot of complaints of redefition of types after compiling? Is there a straightforward way to do it? I have googled about nested header files but this gives me rather complicated articles. I was wondering if there is a simple way to do this?
Share
Sure there is. Use include guards.
This way you can include file1.h over and over. In particular, you should always use include guards if a header defines things.
As a side note, if you don’t need the details of the struct (that is, it should be an opaque type), you can use an incomplete type and just say
struct yourstruct;at the top.