I have a namesapce ns. Within ns I defined a class and a function. I further defined the function to be a friend of the class so that the function can access a private variable of the class. In the main program, I succeeded in calling the function without quflifying it with ns::. Please see the code for details.
#include <iostream>
namespace ns {
class MyClass;
void print_fn(const MyClass & c);
}
namespace ns {
class MyClass {
private:
int _a;
public:
MyClass(int a): _a(a) {}
friend void print_fn(const MyClass & c);
};
void print_fn(const MyClass & c){
std::cout << c._a << std::endl;
}
}
int main()
{
ns::MyClass c(10);
print_fn(c);
return 0;
}