#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()?
诚心求教……