When defining class member functions in the source file, we can use using namespace ns1::ns2::...::nsx to avoid having to fully qualify function names.
E.g.
// Header file - foo.hpp
namespace ns1::ns2 {
class FooClass {
void fooMethod();
}
}
// Source file - foo.cpp
#include "foo.hpp"
using namespace ns1::ns2;
void FooClass::fooMethod() {
// do something
}
However, I recently got into a confusion when trying to define a global function.
See the following example.
// Header file - bar.hpp
namespace ns1::ns2{
void barFunction(); // <-- compile error: undefined reference
}
// Source file - bar.cpp
#include "bar.hpp"
using namespace ns1::ns2;
void barFunction(){
// do something
}
I was expecting the compiler to deduce the definition in bar.cpp to be the definition of ns1::ns2::barFunction defined in bar.hpp. But it doesn't do that. (Everything works fine if I use the fully qualified name in the definition).
Is this the expected behavior or am I doing something wrong?