Forward declaration of nested types/classes in C++

Calmarius picture Calmarius · Jun 4, 2009 · Viewed 56.4k times · Source

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?

Answer

Adam Rosenfield picture Adam Rosenfield · Jun 4, 2009

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.