Why can’t I forward-declare a class in a namespace using double colons?

You’re getting correct answers, let me just try re-wording:

class Namespace::Class;

Why do I have to do this?

You have to do this because the term Namespace::Class is telling the compiler:

…OK, compiler. Go find the
namespace named Namespace, and within
that refer to the class named Class.

But the compiler doesn’t know what you’re talking about because it doesn’t know any namespace named Namespace. Even if there were a namespace named Namespace, as in:

namespace Namespace
{
};

class Namespace::Class;

it still wouldn’t work, because you can’t declare a class within a namespace from outside that namespace. You have to be in the namespace.

So, you can in fact forward declare a class within a namespace. Just do this:

namespace Namespace
{
    class Class;
};

Leave a Comment