永发信息网

C++中友元函数的一点疑问

答案:1  悬赏:20  手机版
解决时间 2021-04-28 08:42
  • 提问者网友:斑駁影
  • 2021-04-27 12:56

#include "iostream"

using namespace std;

class Point
{
public:
Point(float x=0,float y=0);
void setPoint(float,float);
float getX() const
{
return x;
}

float getY() const
{
return y;
}

friend ostream& operator <<(ostream&,const Point&);

protected:
float x,y;
};

ostream& operator<<(ostream& output,const Point& p)
{
output<<"["<<p.x<<","<<p.y<<"]"<<endl;
return output;
}

看教材说是友元函数可以访问类的私有成员,为何上面粗体中的p.x和p.y在编译时提示错误,必须改为p.getX()和p.getY()?

诚心求教……

最佳答案
  • 五星知识达人网友:你可爱的野爹
  • 2021-04-27 13:15
vc6?
它的iostream文件有bug

#include "iostream"

using namespace std;

修改为

#include iostream.h


我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯