Search code examples
c++classnestedforward-declaration

Forward declaration of nested types/classes in C++


I recently got stuck in a situation like this:

class A
{
public:
    typedef struct/class {…} B;
…
    C::D *someField;
}

class C
{
public:
    typedef struct/class {…} D;
…
    A::B *someField;
}

Usually you can declare a class name:

class A;

But you can't forward declare a nested type, the following causes compilation error.

class C::D;

Any ideas?


Solution

  • You can't do it, it's a hole in the C++ language. You'll have to un-nest at least one of the nested classes.