I can declare a pointer to a class that hasn’t yet been defined, like this:
class A ;
A* p ;
But how do I do this for a nested class? I want to do this:
class A ;
class A::B ; // error: 'B' in class 'A' does not name a type
A::B* p ;
But it doesn’t compile (using g++ 4.5.2). Is there any way to make this work?
There are a number of parts of C++03 that disallow forward declarations of nested classes. In particular, § 7.1.5.3 Elaborated type specifiers:
In short, when an identifier is scoped, the compiler must try to resolve the identifier. When the scope is a class, the compiler must look up the declaration for the identifier in the outer class. When the outer class hasn’t yet been defined, this can’t be done and the result is an ill-formed program.