在学习C++的友元时,遇到一个问题:两个类互相调用时报错
如图:
#include <iostream>
#include <string>
using namespace std;
//成员函数作友元
class Home;//先声明Home类,之后再实现,方便写在前面的类GirlFriend调用
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
class GirlFriend
{
public:
GirlFriend();
GirlFriend(const GirlFriend& tmp);
~GirlFriend();
void visit();
public:
Home* home = NULL;
};
GirlFriend::GirlFriend()
{
home = new Home; //用指针home接收一个在堆区创建的Home类对象
}
/*------------------------------------------------------------------------------*/
//为了避免浅拷贝带来的重复释放问题,需要自己编写拷贝构造函数实现深拷贝
GirlFriend::GirlFriend(const GirlFriend& tmp)
{
home = new Home(*tmp.home);
}
/*------------------------------------------------------------------------------*/
GirlFriend::~GirlFriend()
{
if (home != NULL)
{
delete home;
home = NULL;
}
}
/*------------------------------------------------------------------------------*/
void GirlFriend::visit()
{
cout << "Your girl is visiting:" << home->m_LivingRoom << endl;
//因为visit()函数作为了Home类的友元,所以能够访问其私有成员m_LivingRoom
cout << "Your girl is visiting:" << home->m_BedRoom << endl;
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
class Home
{
//把GirlFriend类中的成员函数visit()作为Home类的友元,以便访问Home类中的私有属性
friend void GirlFriend::visit();
public:
Home();
public:
string m_LivingRoom;
private:
string m_BedRoom;
};
/*------------------------------------------------------------------------------*/
Home::Home()
{
m_BedRoom = "BedRoom";
m_LivingRoom = "LivingRoom";
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void test10()
{
GirlFriend myGirl;
myGirl.visit();
}
/*------------------------------------------------------------------------------*/
int main()
{
test10();
system("pause");
return 0;
}
报错如下
再参考https://blog.csdn.net/strive_max/article/details/127947510的方法以后,发现原来是在类的声明和定义之间,类是一个不完全类型,而不完全类型只能用于“定义指向该类型的指针及引用,或者用于声明(而不是定义)使用该类型作为形参类型或返回类型的函数”
同时在参考https://blog.csdn.net/chuomei5332/article/details/109675366和https://blog.csdn.net/qq_43915356/article/details/107577450两位大佬的说明后,将代码修改成以下状态(将除开不完全类型能用的操作之外的函数全部移到类的定义之后)
#include <iostream>
#include <string>
using namespace std;
//成员函数作友元
class Home;//先声明Home类,之后再实现,方便写在前面的类GirlFriend调用
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
class GirlFriend
{
public:
GirlFriend();
GirlFriend(const GirlFriend& tmp);
~GirlFriend();
void visit();
public:
Home* home = NULL;
};
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
class Home
{
//把GirlFriend类中的成员函数visit()作为Home类的友元,以便访问Home类中的私有属性
friend void GirlFriend::visit();
public:
Home();
public:
string m_LivingRoom;
private:
string m_BedRoom;
};
/*------------------------------------------------------------------------------*/
Home::Home()
{
m_BedRoom = "BedRoom";
m_LivingRoom = "LivingRoom";
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/*----------------------------不完全类型不允许的操作----------------------------*/
GirlFriend::GirlFriend()
{
home = new Home; //用指针home接收一个在堆区创建的Home类对象
}
/*------------------------------------------------------------------------------*/
//为了避免浅拷贝带来的重复释放问题,需要自己编写拷贝构造函数实现深拷贝
GirlFriend::GirlFriend(const GirlFriend& tmp)
{
home = new Home(*tmp.home);
}
/*------------------------------------------------------------------------------*/
GirlFriend::~GirlFriend()
{
if (home != NULL)
{
delete home;
home = NULL;
}
}
/*------------------------------------------------------------------------------*/
void GirlFriend::visit()
{
cout << "Your girl is visiting:" << home->m_LivingRoom << endl;
//因为visit()函数作为了Home类的友元,所以能够访问其私有成员m_LivingRoom
cout << "Your girl is visiting:" << home->m_BedRoom << endl;
}
/*----------------------------不完全类型不允许的操作----------------------------*/
void test10()
{
GirlFriend myGirl;
myGirl.visit();
}
/*------------------------------------------------------------------------------*/
int main()
{
test10();
system("pause");
return 0;
}
果然,再次运行,成功,没有报错
总结:
不完全类型只能用于“定义指向该类型的指针及引用,或者用于声明(而不是定义)使用该类型作为形参类型或返回类型的函数”
如果在A类和B类要相互调用时,要注意方法实现的位置,方法可以先声明在前面,然后在要调用的类的定义之后再实现