tags:

views:

77

answers:

1

If you have ns2 as a child namespace of ns1, and you use ns1 classes inside a header for an ns2 class, do you need to explicitly decalre it as you would when ns1 & ns2 are unrelated?

e.g

button.h

namespace ns1
{
 class Button
 {
  ...
 };
}

dialog.h

include "button.h"
namespace ns1
{
 namespace ns2
 {
  class TestDialog
  {
   Button *pButton;
  };
 }
}

Should that be right? It seems I have to change dialog.h to be:

namespace ns1
{
 ----->class Button;
 namespace ns2
 {

But I'm not quite sure why. Do namespaces not inherit? If I don't make this change, I get linker errors about "unresolved symbol ns1::ns2:Button::...".

+1  A: 

All functions, classes (types), vars, etc. declared in a namespace will be available (without prefix) in all sub namepaces and so on. So when namespace n2 is defined in n1 all code in n2 can use n1 code without prefix.

VDVLeon
Then why do I get linker errors about unresolved "symbol ns1::ns2::Button" if I don't add that extra line?
John
ns1::n2::Button does not exists, ns1::Button does. I said that inside ns2 you can use n1 code. But outside you have to give the correct prefix.
VDVLeon
Well I have code like the example given, and it fails to link unless I make the change shown, which is using the ns1 class inside ns2.
John