Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reference an external C++ namespace from within a nested one?

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?

like image 962
mph Avatar asked Apr 29 '09 17:04

mph


2 Answers

Use global scope resolution:

::nsB::TheFunctionInNsB()
like image 55
i_am_jorf Avatar answered Sep 27 '22 20:09

i_am_jorf


#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;
}
like image 45
Michael Burr Avatar answered Sep 27 '22 20:09

Michael Burr