Forward declaration is a handy mechanism for reducing the number of files you include in your headers. It is especially useful if your header file needs to be used by other classes/files.
However, suppose you try to forward declare classes within a given namespace:
class A::B::CClassName;
You’ll probably encounter the following error message:
namespace "A::B" has no tag member named "CClassName"
There are two ways of resolving this issue:
- Define the namespace with the “using namespace” keyword and then declare the class:
using namespace A::B;class CClassName;
The “using namespace” keyword in the header file is generally not preffered, because it might have an adverse effect on other files?including the header file.
- Define the forward declaration like this:
namespace A{ namespace B { class CClassName; }}
The second method may take up more code lines, but it’s a good method of forward declaration for classes with namespaces.