I'm trying to access a private member of a class from a friend function, but I keep getting an error about x
being a private class of B
.
I know a particular solution may be to just friend
the function inside B
, but I would like a more generic solution, in case I need more friend functions in A
that need access to B
but don't want to explicitly friend
them in B.
#include <iostream>
using namespace std;
class B {
private:
int x = 10;
friend class A;
};
class A {
private:
B b;
public:
friend ostream& operator<< (ostream&, A&);
};
ostream& operator<< (ostream& out, A& a){
out << a.b.x << endl;
return out;
}
int main() {
A a;
cout << a;
return 0;
}