Code error

Can someone please tell me what's wrong with my error and why it's not working

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
 #include <iostream>

using namespace std;

class A
{
	int valuea;

public:
	A() {}
	A(int x) { valuea = x; }
	void convert(B x)
	{
		valuea = x.valueb;
	}
	int getValue() { return valuea; }
};
class B
{
	int valueb;
public:
	B() { }
	B(int x) { valueb = x; }
	friend A;
};

int main()
{
	A a(1);
	B b(2);

	a.convert(b);

	cout << a.getValue() << endl;
}
class B should define before class A, and class A is class B's firend class, so you should define "class A;" before class B.
Like this:

#include <iostream>
using namespace std;

class A;
class B
{
int valueb;
public:
B() { }
B(int x) { valueb = x; }
friend A;
};
class A
{
int valuea;

public:
A() {}
A(int x) { valuea = x; }
void convert(B x)
{
valuea = x.valueb;
}
int getValue() { return valuea; }
};

int main()
{
A a(1);
B b(2);

a.convert(b);

cout << a.getValue() << endl;
}
Thank you so much
Topic archived. No new replies allowed.