views:

412

answers:

3

I have two namespaces defined in the default/"root" namespace, nsA and nsB. nsA has a sub-namespace, nsA::subA. When I try referencing a function that belongs to nsB, from inside of nsA::subA, I get an error:

undefined reference to `nsA::subA::nsB::theFunctionInNsB(...)'

Any ideas?

+7  A: 

Use global scope resolution:

::nsB::TheFunctionInNsB()
jeffamaphone
+4  A: 
#include <stdio.h>

namespace nsB {
    void foo() {
        printf( "nsB::foo()\n");
    }
}

namespace nsA {
    void foo() {
        printf( "nsA::foo()\n");
    }

    namespace subA {
        void foo() {
            printf( "nsA::subA::foo()\n");
            printf( "calling nsB::foo()\n");

            ::nsB::foo();      // <---  calling foo() in namespace 'nsB'
        }
    }
}

int main()
{
    nsA::subA::foo();

    return 0;
}
Michael Burr
Thanks Michael -- both of these answer my question perfectly, jeffamaphone got it first so I'll give him the check. Your illustration will be good to have here for future surfers.
mikeh
+1 for giving a code sample. That made the answer more clear
Edison Gustavo Muenz
+1  A: 

Need more information to explain that error. The following code is fine:

#include <iostream>

namespace nsB {
    void foo() { std::cout << "nsB\n";}
}

namespace nsA {
    void foo() { std::cout << "nsA\n";}
    namespace subA {
        void foo() { std::cout << "nsA::subA\n";}
        void bar() {
            nsB::foo();
        }
    }
}

int main() {
    nsA::subA::bar();
}

So, while specifying the global namespace solves your current problem, in general it is possible to refer to symbols in nsB without it. Otherwise, you'd have to write ::std::cout, ::std::string, etc, whenever you were in another namespace scope. And you don't. QED.

Specifying the global namespace is for situations where there's another nsB visible in the current scope - for instance if nsA::subA contained its own namespace or class called nsB, and you want to call ::nsbB:foo rather than nsA::subA::nsB::foo. So you'd get the error you quote if for example you have declared (but not defined) nsA::subA::nsB::theFunctionInNsB(...). Did you maybe #include the header for nsB from inside namespace subA?

Steve Jessop
"Did you maybe #include the header for nsB from inside namespace subA?" -- indeed, I had. Great catch, and good explanation.
mikeh