This is fixed like this:
file.cpp remains unchanged.
The following is written to file.h :
class A; namespace NS { void f(const A &obj); } class A { private: int data; friend void NS::f(const A &obj); }
Why didn't your option work?
First, the compiler did not know what NS : a namespace or class, or something else.
However, even if you add it over the namespace NS {} class, it will not work anyway, but this time the error will be different: error: 'void NS::f(const A&)' should have been declared inside 'NS' (c) GCC .
Be f in the same namespace as the class, friend void f(const A &); would work without a function declaration. (This line itself would be a function declaration, although it would be possible to reach its normal (re) declaration only through an argument-dependent lookup.)
Why is that? Apparently the developers of the standard decided that declaring functions in arbitrary namespaces from classes in other namespaces is a brute force.