0

I need to use a struct defined in A.h as both a parameter to a method as well as a data element to another struct in B.h. (I can't use a pointer to StructA in StructB because the data to which it would point eventually changes, and I need to preserve the original data in a local copy in StructB.)

A.h:

struct StructA {
...
}

B.h

#include "A.h" 
struct StructA;

class B {
  method foo (StructA &structA);
  struct StructB {
     ...
     StructA structA
  }

By including A.h in B.h, isn't the declaration of StructA available in B.h? However, the compiler complains that "field structA has incomplete type".

If I don't forward declare StructA, the compiler then complains StructA is not declared in method foo. So, obviously the declaration isn't available in B.h.

I'm missing something fundamental here, but I can't figure it out and it's driving me insane! Please help!

Brian Spisak
  • 63
  • 1
  • 4

1 Answers1

0

The #include "A.h" does indeed supply B.h with the complete type, however my guess is that you have a circular include, that's why it doesn't work. Does A.h include B.h (directly or not)?

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Yes, apparently (indirectly) it is. So the solution is to pull the declaration for StructA and put it in it's own header file? – Brian Spisak Oct 25 '13 at 07:50
  • It wasn't by itself in A.h and the dependencies were obfuscated by multiple includes. The bottom line is that it was a circular dependency. Although I saw that mentioned elsewhere here with respect to similar problems, I didn't immediately see how it caused the problem. To be honest, it's still not crystal clear to me, so I'll have to review and make sure I understand this and adopt some best practices to avoid this kind of thing in the future. – Brian Spisak Oct 25 '13 at 08:07
  • @BrianSpisak read this - http://stackoverflow.com/questions/14909997/why-arent-my-include-guards-preventing-recursive-inclusion-and-multiple-symbol – Luchian Grigore Oct 25 '13 at 08:30