when two class are friend of each other

next class access property of previous class but previous class can't access member of next class when both class are friend of each other

class previous
{
friend class next;

int x;
public:
A(){cin>>x;}
void set()
{
cin>>x;
}
void display(next a)
{

cout<<a.x; // error: x is not a member of next
}
};

class next
{
friend class previous;

public:
int x;
B(){cin>>x;}
void set()
{

}
void display(previous a)
{
cout<<a.x;
}
};
anyone can solve this problem
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class B; //forward declaration
class A
{
	friend class B;

	int x;
	public:
	A(){cin>>x;}
	void set()
	{
		cin>>x;
	}
	void display(B a); //just declaration
};

class B
{
	friend class A;

	public:
	int x;
	B(){cin>>x;}
	void set()
	{

	}
	void display(A a)
	{
		cout<<a.x;
	}
};

void A::display(B a) //now we have the definition of B, so can code the function
{
	cout<<a.x;
}
Last edited on
Read this (Friend classes):

http://www.cplusplus.com/doc/tutorial/inheritance/


What you need is a forward declaration of next:

1
2
3
4
5
6
class next; // forward declaration

class previous
{
friend class next;
...


Read this:
http://en.cppreference.com/w/cpp/language/class
Topic archived. No new replies allowed.