开发者

Structures inside Structures C/C++

I just recently switched over from Java prog开发者_如何学编程ramming to C/C++ programming so forgive me if this is a silly question:

I have a header file which is composed of a structure which includes 2 structures from another header file in a different location. It looks something like this:

struct A {
   struct B variable1;
   struct C *variable2;
   ... more variable declarations here.
};

The header files which contains the declarations for struct B and struct C are not included in this specific header file, but even if I do include them it makes no difference - in both scenarios at compilation time I get

error: field 'variable1' has incomplete type

I am just wondering if anyone knows what this could be from? Thank you!


If you don't include the definition of struct B you should get the mentioned error, since the size of the structure is then unknown. The type of struct C to which you only have a pointer could just be pre-declared:

struct C;

If you do include the file with the definition of struct B, you should not get the error, so if you still do that points at some other error in your code.


Could it be the order the structs are defined? Struct B need be defined and ready before Struct A.

My test was: works

struct b {
  int y;
};

struct a {
  int x;
  struct b c;
};

same error as you:

struct a {
  int x;
  struct b c;
};

struct b {
  int y;
};


To declare an instance of struct B, the definition of struct B must be complete. You state that

The header files which contains the declarations for struct B and struct C are not included in this specific header file

At this point the compiler doesn't know what struct B looks like, so the type is incomplete; thus, it will reject any declaration that tries to create an instance of struct B, such as the declaration for variable1. You can declare a pointer to an incomplete type, as you do with variable2, since struct pointer types all have the same size and representation.

Then you state

but even if I do include them it makes no difference

which strongly suggests (to me, anyway) a circular dependency between struct A and struct B, which is bad juju. struct A cannot be complete until struct B is complete, and struct B cannot be complete until struct A is complete. Changing variable1 to be a pointer to struct B should be enough to break that dependency.

If that is the case, you may want to revisit your design.


struct A is defined here, and it must have a known size. The compiler has to know what is the size of struct B to calculate the size of struct A, so if struct B is not yet defined, you will not be able to use it in struct A.

You don't have that problem with struct C*, since struct A only keeps a pointer to struct C, and the size of a pointer is known to the compiler. But you need to use forward declaration for it if it is not defined before struct A:

struct B {
   int b;
}
struct C;
struct A {
   struct B variable1;  // valid
   struct C *variable2; // valid
   struct D variable3;  // invalid
   struct D* variable4; // invalid
};

struct C {
    int c;
}

struct D {
    int d;
}


Try forward declaration :

struct B;
struct C;  
struct A {
   struct B variable1;
   struct C *variable2;
   ... more variable declarations here.
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜