When I compile the following code,
#include <iostream> using namespace std; class class1;//forward declaration class class1{ int var; public: void setter(int); friend void getter(class1 o1); }; class class2{ public: void getter(class1 o1) { o1.var; } }; void class1::setter(int v) { var =v; } int main() { int val; class1 o1; class2 o2; cout<<"Enter value\n"; cin>>val; o1.setter(val); cout <<"value:\n"<<o2.getter(o1); return 0; } I'm getting the following errors, G:\C++ projects\private access specifier\main.cpp|6|error: 'int class1::var' is private| G:\C++ projects\private access specifier\main.cpp|16|error: within this context| G:\C++ projects\private access specifier\main.cpp|32|error: no match for 'operator<<' (operand types are 'std::basic_ostream' and 'void')|
Here I need to access a private member 'var' form class1 with a function 'getter' in class2 I know that there is another simpler way to get & set values but I'm here to understand the working of friend function. Kindly clarify my doubt as I'm new to C++