I wrote the following code:
#include<iostream>
using namespace std;
class foo {
private:
int i;
public:
foo(): i(1) { }
friend int func1(int i) {
return 0;
}
friend int func2(foo &f) {
return f.i;
}
};
int main()
{
foo f;
cout << func2(f) << endl;
cout << func1(1) << endl;
return 0;
}
But it cannot compile with the following errors:
ss.cpp: In function ‘int main()’:
ss.cpp:28:17: error: ‘func1’ was not declared in this scope
When I deleted this line:
cout << func1(1) << endl;
It compiled successfully
Does that means if I want to define a friend function in a class and call it in global namespace, It must have some relationship with the class ? If so, what's the rule in detail ?
My compiler is g++-4.7.2