There is such code:
#include <iostream>
class A{
public:
friend void fun(A a){std::cout << "Im here" << std::endl;}
friend void fun2(){ std::cout << "Im here2" << std::endl; }
friend void fun3();
};
void fun3(){
std::cout << "Im here3" << std::endl;
}
int main()
{
fun(A()); // works ok
//fun2(); error: 'fun2' was not declared in this scope
//A::fun2(); error: 'fun2' is not a member of 'A'
fun3(); // works ok
}
How to access function fun2()?
Although your definition of
fun2does define a “global” function rather than a member, and makes it afriendofAat the same time, you are still missing a declaration of the same function in the global scope itself.That means that no code in that scope has any idea that
fun2exists.The same problem occurs for
fun, except that Argument-Dependent Lookup can take over and find the function, because there is an argument of typeA.I recommend instead defining your functions in the usual manner:
Notice now that everything works (except
fun3because I never defined it).