TITLE: Type tags, scope, and forward declarations PROBLEM: Alan Harper In a project I am working on we have tools which generate C ".h" files which we then include in our project. We are trying to switch over to compiling under C++ (though we will not be using many of the features of C++), and I am finding that at least one C++ compiler deals with forward "struct *" references differently than C does. The code looks like: /* * If I include the following 2 lines, this will compile under Metrowerks * C++, but won't w/o these lines. * * struct firstTypeTag; * struct secondTypeTag; */ typedef struct twoPtrsTypeTag { struct firstTypeTag *ptr1; struct secondTypeTag *ptr2; } twoPtrsType; RESPONSE: clamage@Eng.Sun.COM (Steve Clamage), 11 Jan 95 Right. This is a difference between C and C++. In addition, the exact meaning of your declaration of struct twoPtrsTypeTag in C++ has been clarified by recent decisions of the C++ Committee. Thus, it is possible to get different results from different C++ compilers. Eventually, all C++ compilers will give the same result as the one you don't like. :-) Under current language rules, if the first appearance of an "incomplete declaration", as in your example firstTypeTag and secondTypeTag, is inside a class, it is a forward declaration of a nested class name. That is, you have forward-declared the type names twoPtrsTypeTag::firstTypeTag and twoPtrsTypeTag::secondTypeTag. The members ptr1 and ptr2 have a type different from the file-scope structs called firstTypeTag and secondTypeTag. If you include the file-scope declarations in your comment, then the declarations inside struct typePtrsTypeTag will refer to the file-scope structs, as you want. Happily, this rule is the same as in C. Moral: In both C and C++, declare file-scope names at file scope, not in any other scope. You will then not have any compatibility problems.