namespace Test{ class A{ public: friend void TestFn1(const A &a){}; }; void TestFn2(const A &a){}; } class B{ public: operator Test::A(){return Test::A();} }; using namespace Test; void TestFn3(const A &a){}
void main(){ B obj; TestFn1(obj); TestFn2(obj);//不需要通过对象调用,因为有using namespace Test; TestFn3(obj); }
[解决办法]
这并不是问题的本质, 下面这段话来自MSDN:
'function' candidate function(s) not accessible
A friend function defined in a class is not supposed to be treated as if it were defined and declared in the global namespace scope. It can, however, be found by argument-dependent lookup.
例子:和你给的基本一样
C/C++ code // C3767e.cpp namespace N { class C { friend void FriendFunc() {} friend void AnotherFriendFunc(C* c) {} }; }
int main() {
using namespace N; FriendFunc(); // 直接编译错误 C3767 error C* pC = new C(); AnotherFriendFunc(pC); // found via argument-dependent lookup } [解决办法]