【发布时间】:2013-05-06 12:06:50
【问题描述】:
这是一个了解如何在 C++ 中使用friend class 的基本程序。
xxx 类有一个使用friend 的类yyy 对象。自上课yyy
在xxx 类之后定义我已经使用forward 声明了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;
}
当我编译程序时,我得到了这个错误:
错误:聚合“yyy y2”类型不完整,无法定义
我参考了这个链接 recursive friend classes 有人这样回答 https://stackoverflow.com/a/6158833/2168706
我正在遵循这种方法,但我仍然无法解决 问题。如果存在,请提供解决方案,请告诉我 如果我在代码中的任何一点错了。
【问题讨论】:
标签: c++ friend friend-class