oqqkid1234 2015-04-11 13:18 采纳率: 60%
浏览 1953
已采纳

关于友元函数的小问题

#include //声明类Point
using namespace std;
class Point
{
public:
Point(float x=0,float y=0); //有默认参数的构造函数
void setPoint(float ,float); //设置坐标值
float getX( )const {return x;} //读x坐标
float getY( )const {return y;} //读y坐标
friend ostream& operator<<(ostream &,const Point &);
//重载运算符“<<”
protected: //受保护成员
float x, y;
};
//下面定义Point类的成员函数
Point::Point(float a,float b) //Point的构造函数
{ //对x,y初始化
x=a;
y=b;
}
void Point::setPoint(float a,float b) //设置x和y的坐标值
{ //为x,y赋新值
x=a;
y=b;
}
//重载运算符“<<”,使之能输出点的坐标
ostream& operator<<(ostream &cout, const Point &p)
{
cout<<"["<<p.x<<","<<p.y<<"]"<<endl;
return cout;
}
class Circle:public Point //circle是Point类的公用派生类
{
public:
Circle(float x=0,float y=0,float r=0); //构造函数
void setRadius(float ); //设置半径值
float getRadius( )const; //读取半径值
float area ( )const; //计算圆面积
friend ostream &operator <<(ostream &,const Circle &); //重载运算符“<<”
private:
float radius;
};
//定义构造函数,对圆心坐标和半径初始化
Circle::Circle(float a,float b,float r):Point(a,b),radius(r){}
//设置半径值
void Circle::setRadius(float r){radius=r;}
//读取半径值
float Circle::getRadius( )const {return radius;}
//计算圆面积
float Circle::area( )const
{
return 3.14159*radius*radius;
}
//重载运算符“<<”,使之按规定的形式输出圆的信息
ostream &operator <<(ostream &output,const Circle &c)
{
output<<"Center=["<<c.x<<","<<c.y<<"],r="<<c.radius<<",area="<<c.area( )<<endl;
return output;
}
int main( )
{
Circle c(3.5,6.4,5.2); //建立Circle类对象c,并给定圆心坐标和半径
cout<<"original circle:\nx="<<c.getX()<<", y="<<c.getY()<<", r="<<c.getRadius( )<<", area="<<c.area( )<<endl; //输出圆心坐标、半径和面积
c.setRadius(7.5); //设置半径值
c.setPoint(5,5); //设置圆心坐标值x,y
cout<<"new circle:\n"<<c; //用重载运算符“<<”输出圆对象的信息
Point &pRef=c; //pRef是Point类的引用变量,被c初始化
cout<<"pRef:"<<pRef; //输出pRef的信息
return 0;
}

报错内容是(友元函数不能访问保护成员???)
G:\c_files\try_2.cpp(506) : error C2248: 'x' : cannot access protected member declared in class 'Point'
G:\c_files\try_2.cpp(490) : see declaration of 'x'
G:\c_files\try_2.cpp(506) : error C2248: 'y' : cannot access protected member declared in class 'Point'
G:\c_files\try_2.cpp(490) : see declaration of 'y'
G:\c_files\try_2.cpp(534) : error C2248: 'x' : cannot access protected member declared in class 'Point'
G:\c_files\try_2.cpp(490) : see declaration of 'x'
G:\c_files\try_2.cpp(534) : error C2248: 'y' : cannot access protected member declared in class 'Point'
G:\c_files\try_2.cpp(490) : see declaration of 'y'
G:\c_files\try_2.cpp(534) : error C2248: 'radius' : cannot access private member declared in class 'Circle'
G:\c_files\try_2.cpp(518) : see declaration of 'radius'
G:\c_files\try_2.cpp(539) : warning C4305: 'argument' : truncation from 'const double' to 'float'
G:\c_files\try_2.cpp(539) : warning C4305: 'argument' : truncation from 'const double' to 'float'
G:\c_files\try_2.cpp(545) : error C2593: 'operator <<' is ambiguous
执行 cl.exe 时出错.
另有,将point类中重载函数写成inline就不报错了,为什么?

  • 写回答

3条回答

  • devmiao 2015-04-11 15:07
    关注

    友元函数只能访问this,不能访问另一个对象

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器