Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

referencing something from global namespace?

Tags:

c++

namespaces

This is really trivial but I'm getting an error I didn't expect.

I have some code which is inside of a namespace

the following is some pseudocode that represents the structure of my code:

namespace A {
    void init() {
        initialize_kitchen_sink();
    }
    #include "operations.h" // declares shake_and_bake()
    void foo() {            
        shake_and_bake();
    }
    void cleanup() {
        // do nothin' cuz i'm a slob
    }
}

error:

undefined reference to `A::shake_and_bake`
like image 899
Steven Lu Avatar asked Dec 13 '22 11:12

Steven Lu


2 Answers

Turns out moving the #include outside the namespace will fix it.

As it was, the include would be in effect declaring all of the functions in operations.h inside the A namespace. Then it would search in vain for the implementations.

I figure instead of deleting my entire post i may as well leave it for that minute possibility that someone else may stumble upon a similar problem and be enlightened.

like image 191
Steven Lu Avatar answered Dec 31 '22 04:12

Steven Lu


To answer precisely to your question, you can reference something from the global namespace by using :: as your first statement as in :

 void foo() {            
        ::shake_and_bake();
    }

Of course, your answer, for this special case is right though.

like image 39
Dinaiz Avatar answered Dec 31 '22 02:12

Dinaiz