#include<iostream>
#include<string>
using namespace std;
class Bulding
{
friend void Goodgay::visit();
public:
Bulding()
{
m_room = "正在访问客厅";
b_room = "正在访问卧室";
}
string m_room;
private:
string b_room;
};
class Goodgay
{
public:
Goodgay();
void visit();
Bulding* bulding;
};
Goodgay::Goodgay()
{
bulding = new Bulding;
}
void Goodgay::visit()
{
cout << bulding->m_room << endl;
cout << bulding->b_room << endl;//显示不可访问
}
void test()
{
Goodgay b;
b.visit();
}
int main()
{
test();
return 0;
}
在friend void Goodgay::visit();这里的时候,编译器不识别Goodgay,所以报错,代码修改如下:
如有帮助,请采纳一下,谢谢。
#include<iostream>
#include<string>
using namespace std;
class Bulding;
class Goodgay
{
public:
Goodgay();
void visit();
Bulding* bulding;
};
class Bulding
{
friend void Goodgay::visit();
public:
Bulding()
{
m_room = "正在访问客厅";
b_room = "正在访问卧室";
}
string m_room;
private:
string b_room;
};
Goodgay::Goodgay()
{
bulding = new Bulding;
}
void Goodgay::visit()
{
cout << bulding->m_room << endl;
cout << bulding->b_room << endl;//显示不可访问
}
void test()
{
Goodgay b;
b.visit();
}
int main()
{
test();
return 0;
}