views:

1671

answers:

2

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?

+14  A: 

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.

Adam Rosenfield
A: 

I would not call this an answer, but nonetheless an interesting find: If you repeat the declaration of your struct in a namespace called C, everything is fine (in gcc at least). When the class definition of C is found, it seems to silently overwrite the namspace C.

namespace C {
    typedef struct {} D;
}

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

class C
{
public:
   typedef struct/class {...} D;
...
   A::B *someField;
}
nschmidt
I tried this with cygwin gcc and it doesn't compile if you try to reference A.someField. C::D in the class A definition actually refers the the (empty) struct in the namespace, not the struct in the class C(BTW this doesn't compile in MSVC)
Dolphin
It gives the error: "'class C' redeclared as different kind of symbol"
Calmarius
Looks like a GCC bug. It seems to think a namespace name can hide a class name in the same scope.
Johannes Schaub - litb