这是一个基本程序,用于了解如何在C++中使用friend class
。转发类声明的朋友类不编译
类xxx
有一个类yyy
对象使用friend
。由于类yyy
被定义在类xxx
之后,我宣布类yyy
使用正向 声明。
#include<iostream>
using std::cout;
using std::endl;
class yyy; //Forward Declaration of class yyy
class xxx{
private:
int a;
public:
xxx(){a=20;yyy y2;y2.show();} //Error//
void show(){cout<<"a="<<a<<endl;}
friend class yyy; //Making class yyy as freind of class xxx
};
class yyy{
private:
int b;
public:
yyy(){b=10;}
void show(){cout<<"b="<<b<<endl;}
};
int main(int argc, char *argv[])
{
xxx x1; //creating xxx object and calling constructor
x1.show();
return 0;
}
当我编译的程序中,我得到这个错误:
error: aggregate ‘yyy y2’ has incomplete type and cannot be defined
我提到了这个链接 recursive friend classes ,有人回答了这样 https://stackoverflow.com/a/6158833/2168706
我下面这个方法,但我仍然无法解决 问题。请提供一个解决方案,如果有任何问题,请告诉我 如果我在代码中的任何一点都是错误的。
它不起作用,因为y.Show()在您调用它之前尚未声明。 Haroogan的解决方案将解决这个问题。 – 2013-05-06 12:11:17
当然,如果该实现保留在标题中,则可能会导致链接器错误;你应该把它放在一个.cpp文件中或者将它标记为内联。 – 2013-05-06 12:11:58
感谢Harogan的回复。 – 2013-05-06 13:26:28