I want to define two classes, A and B. A has a data member which is a Class B object and is in-class initialised. A also has a method to retrieve the value in this B type data member and this method would be declared as a friend method in B. Here is my code:
class A{ public: int getBValue(); private: B b=B(1); }; class B{ public: friend int A::getBValue(); B(int i):value(i){} private: int value; }; int A::getBValue(){ return b.value; } And unsurprisingly the compilation had failed because of unknown type B in class A definition. I had tried to swap the definitions of A and B in the source and the result was even worse. Is there a possible way to resolve this cross reference issue between A and B?
friendrequirement and provide a getter instead.