By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,719 Members | 1,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,719 IT Pros & Developers. It's quick & easy.

Nested namespaces

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.