Code:
#include<iostream>
using namespace std;
class MyClassOne;
class MyClassTwo
{
int myInteger;
float myFloat;
public:
void SetData(int myIntegerParameter, float myFloatParameter)
{
myInteger = myIntegerParameter;
myFloat = myFloatParameter;
}
void Show(MyClassOne myObjectParameter)
{
cout<<"MyClassOne..."<<"\n";
cout<<myObjectParameter.myInteger<<"\n";
cout<<myObjectParameter.myFloat<<"\n";
cout<<"MyClassTwo..."<<"\n";
cout<<myInteger<<"\n";
cout<<myFloat<<"\n";
}
};
class MyClassOne
{
int myInteger;
float myFloat;
public:
void SetData(int myIntegerParameter, float myFloatParameter)
{
myInteger = myIntegerParameter;
myFloat = myFloatParameter;
}
friend void MyClassTwo :: Show(MyClassOne);
};
int main()
{
MyClassOne myObjectOne;
myObjectOne.SetData(10, 10.5);
MyClassTwo myObjectTwo;
myObjectTwo.SetData(20, 20.5);
myObjectTwo.Show(myObjectOne);
return 0;
}
Error Message:
1>friend_function.cpp(22) : error C2027: use of undefined type 'MyClassOne'
1>friend_function.cpp(6) : see declaration of 'MyClassOne'
1>friend_function.cpp(22) : error C2228: left of '.myInteger' must have
class/struct/union
1>friend_function.cpp(23) : error C2027: use of undefined type 'MyClassOne'
1>friend_function.cpp(6) : see declaration of 'MyClassOne'
1>friend_function.cpp(23) : error C2228: left of '.myFloat' must have
class/struct/union
1>Generating Code...
1>Build log was saved at "file://Debug\BuildLog.htm"
1>Problem_05___Friend_Function - 4 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
MyClassTwo::Showexpects a complete definition ofMyClassOne.Move the body of the
Showmethod to after the definition ofMyClassOneand it will work.