友元函数却不能访问私有成员,是怎么回事?
#ifndef VECTOR_H_#define VECTOR_H_
#include <iostream>
using namespace std;
class Vector
{
private:
double x;
double y;
double mag;
double ang;
char mode;
//private methods
void set_mag();
void set_ang();
void set_x();
void set_y();
public:
Vector();
Vector(double n1, double n2, char form='r');
void set(double n1, double n2, char form='r');
~Vector();
double xval() const { return x; }
double yval() const { return y; }
double magval() const { return mag; }
double angval() const { return ang; }
void polar_mode();
void rect_mode();
//operator overloading
Vector operator+(const Vector &v) const;
Vector operator-(const Vector &v) const;
Vector operator-() const;
Vector operator*(double n) const;
//friends
friend Vector operator*(double n, const Vector &v);
friend ostream & operator<<(ostream &os, const Vector &v);
};//end class Vector
#endif
//部分函数定义省略
//.........
ostream & operator<<(ostream &os, const Vector &v)
{
if(v.mode == 'r')
os << "(x,y)=(" << v.x << "," << v.y << ")";
else if(v.mode == 'p')
os << "(m,a)=(" << v.mag << "," << v.ang*Rad_to_deg << ")";
else
os << "Vector object mode is invalid.";
return os;
}
这里编译器报错,说不能访问私有成员是怎么回事?明明定义为友元的...