c++ - Class Member Functions and Classes as Friends -
i've written 2 classes: function of first class cannot access private member of second class though function friend second class. found example msdn.microsoft.com there still error: cannot access private member declared in class b
here code msdn:
class b; class { public: int func1(b& b); private: int func2(b& b); }; class b { private: int _b; // a::func1 friend function class b // a::func1 has access members of b friend int a::func1(b&); }; int a::func1(b& b) { return b._b; }//the same error 1 below here int a::func2(b& b) { return b._b; }
when write class a
friend b
there no error want have function want friend class b not whole class a
is compiler fault or code wrong?
i added declaration
class b
(called forward declaration of class
) @ top , compiles. need declare class b
before use parameter
in member functions
of class a
.
here code ->
#include<iostream> class b; class { public: int func1(b& b); private: int func2(b& b); }; class b { private: int _b; // a::func1 friend function class b // a::func1 has access members of b friend int a::func1(b&); }; int a::func1(b& b) { return b._b; }//the same error 1 below here //int a::func2(b& b) { return b._b; } int main(void){ return 0; }
if error still persists maybe compiler @ fault.
Comments
Post a Comment