How to forward declare a class which is in a namespace

Apart to forward-declare the class from within its namespace (as @billz says), remember to either use (prepend) that namespace when referring to the forward-declared class, or add a using clause:

// B.h
namespace Y { class A; } // full declaration of
// class A elsewhere

namespace X {
    using Y::A;   // <------------- [!]
    class B {
        A* a; // Y::A
    };
}

Ref: Namespaces and Forward Class Declarations


To forward declare class type a in a namespace ns1:

namespace ns1
{
    class a;
}

To forward declare a type in multiple level of namespaces:

namespace ns1
{
  namespace ns2
  {
    //....
     namespace nsN
     {
        class a;
     }
    //....    
  }
}

Your are using a a member of consumer which means it needs concrete type, your forward declaration won't work for this case.


For nested namespaces, since C++17, you can do

namespace ns1::ns2::nsN
{
  class a;
}

Tags:

C++

Namespaces