,这样就对。反之,B类先定义,A类后定义,就我这个程序例子就验证不行。是不是这样?那为什么会这样?请解释
#include <IOSTREAM>
using namespace std;
#define pi 3.1415926
class Cylinder;
class Circle
{
private:
double radius;
public:
Circle(double r){radius=r;}
double GetAreaCircle()
{
return pi*radius*radius;
}
friend double Cylinder::GetAreaCylinder();//错误
};
class Cylinder:public Circle
{
private:
double height;
public:
Cylinder(double h,double r):Circle(r)
{
height=h;
}
double GetAreaCylinder()
{
return 2*GetAreaCircle()+2*pi*radius*height;//错误
}
double GetVolumeCylinder()
{
return GetAreaCircle()*height;
}
};
void main()
{
Cylinder cyl(1,2);
cout<<cyl.GetAreaCylinder()<<endl;
cout<<cyl.GetVolumeCylinder()<<endl;
}
friend double Cylinder::GetAreaCylinder();//错误
你好,这句帮我解释下
因为Cylinder类不知道有这个操作?
追答因为在Circle类内,Cylinder尚未定义完,此时仅仅知道有个类名叫Cylinder,但是具体实现是什么样的,此时不知道。也就是说Cylinder::GetAreaCylinder() 这个函数的地址是找不到的
本回答被提问者和网友采纳