469,963 Members | 1,738 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

Nested namespaces

Hi all.

I have several nested namespaces:

A::B::C::MyClass;

Can I write in MyClass.h

namespace A::B::C {
class MyClass {
...
}
}

instead

namespace A {
namespace B {
namespace C {
class MyClass {
...
}
}
}
}

?

When I try the first method, VC8.0 blames me:
Error 1 error C2653: 'A' : is not a class or namespace name in
MyClass.h

Thanks.
Nov 5 '08 #1
1 4979
maverik wrote:
Hi all.

I have several nested namespaces:

A::B::C::MyClass;

Can I write in MyClass.h

namespace A::B::C {
class MyClass {
...
}
}

instead

namespace A {
namespace B {
namespace C {
class MyClass {
...
}
}
}
}

?

When I try the first method, VC8.0 blames me:
Error 1 error C2653: 'A' : is not a class or namespace name in
MyClass.h
Apparently not. What you can do, however, is to define namespace
aliases for those frequently used nested namespaces:

Somewhere you first define

namespace A { namespace B { namespace C {} } }

Then you can do

namespace ABC = A::B::C;

The problem, however, is that you cannot reopen the namespace for adding
some declarations to it using its namespace alias. For instance, this
works:

namespace A { namespace B { namespace C { class D; } } }
namespace ABC = A::B::C;

class ABC::D {
D();
};

int main () {
ABC::D d; // error - private constructor

return 0;
}

But this won't:

namespace A { namespace B { namespace C { } } }
namespace ABC = A::B::C;

namespace ABC { // *** reopen to add class D declaration/definition
class D {
D();
};
}

int main () {
ABC::D d; // error - private constructor
return 0;
}

It will flag the syntax error on line ***. I am not sure if this has
been identified as a defect in the Standard, though.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 5 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

26 posts views Thread by Joshua Beall | last post: by
3 posts views Thread by Rubén Campos | last post: by
1 post views Thread by SideByEach | last post: by
5 posts views Thread by Neil Zanella | last post: by
14 posts views Thread by Tiraman | last post: by
6 posts views Thread by moondaddy | last post: by
37 posts views Thread by Tim N. van der Leeuw | last post: by
78 posts views Thread by Josiah Manson | last post: by
23 posts views Thread by Steven D'Aprano | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.