6

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?

mph
  • 790
  • 7
  • 20

3 Answers3

9

Use global scope resolution:

::nsB::TheFunctionInNsB()
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
5
#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
  • 333,147
  • 50
  • 533
  • 760
2

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
  • 273,490
  • 39
  • 460
  • 699
  • "Did you maybe #include the header for nsB from inside namespace subA?" -- indeed, I had. Great catch, and good explanation. – mph May 01 '09 at 02:26