In the following code class A has a method witch has a parameter an object of class B. Class B has also a method with a parameter an object of class A.
The program does not compile. The foreword declarations of the two classes didn't help! I understand the reason but I don't know how to solve it!
Any help is welcome. If you propose a solution, please give a working version of my code as well.
Thanks a lot!
#include <iostream>
using namespace std;
class A;
class B;
class A
{
public:
int A1;
void display(B objB)
{
cout<<objB.B1<<endl;
}
};
class B
{
public:
int B1;
void display(A objA)
{
cout<<objA.A1<<endl;
}
};
int main()
{
A Aobj;
B Bobj;
Aobj.A1=10;
Bobj.B1=20;
Aobj.display(Bobj);
Bobj.display(Aobj);
return 0;
}
//Example 1:
class B;
class A
{
public:
int A1;
void display( B objB ); //Okay you are just telling it what to expect
};
//Example 2:
class B;
class A
{
public:
int A1;
void display( B objB ){ cout << objB.b1 << endl; } //not okay what is
//inside the class? All we know is we are looking for type B
//but there is no definition of it yet
};