Can not access private variable with friend function

#include <iostream>

using namespace std;
class cmplx;
class B;
class A
{
private:
int a=5;
public:
friend void fun(A);
friend void fun(A,B);
friend void fun(A,B,cmplx);
};
class B
{
private:
int b=7;
public:
friend void fun(B);
friend void fun(A,B);
friend void fun(A,B,cmplx);
};
class cmplx
{
private:
int a,b;
public:
void setdata(int x,int y){a=x;b=y;}
void showdata(){cout<<a<<" "<<b<<endl;}
friend void fun(A,B,cmplx);
};
void fun(cmplx c_new,A a,B b)
{
cout<<"sum is "<<c_new.a+c_new.b+a.a+b.b<<endl;
}
void fun(A a,B b)
{
cout<<"sum is for a and b class "<<a.a+b.b<<endl;
}
void fun(B b)
{
cout<<"num is for b"<<b.b<<endl;
}
void fun(A a)
{
cout<<"num is for a"<<a.a<<endl;
}

int main()
{
cmplx c1;
A a1;B b1;
c1.setdata(4,5);

fun(c1,a1,b1);
fun(a1,b1);
fun(a1);
fun(b1);
return 0;
}

Running this code ,I found the following error
||=== Build: Debug in friend_function (compiler: GNU GCC Compiler) ===|
||In function ‘void fun(cmplx, A, B)’:|
|35|error: ‘int cmplx::a’ is private within this context|
|27|note: declared private here|
|35|error: ‘int cmplx::b’ is private within this context|
|27|note: declared private here|
|35|error: ‘int A::a’ is private within this context|
How to solve these errors and why these errors are showing
|9|note: declared private here|
35|error: ‘int B::b’ is private within this context|
Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

What should I do to solve the issue
The function being called, void fun(cmplx c_new,A a,B b) , hasn't been set as a friend function. You did set this function as a friend, void fun(A,B,cmplx);, but that function doesn't exist.
Thanks a lot
Topic archived. No new replies allowed.