[求助]友元函数不能访问私有成员
两个程序:
#include <iostream.h>
class Date
{
public:
friend ostream& operator <<(ostream &stream,Date &dat);
private:
int Year,Month,Day;
};
ostream& operator <<(ostream &ins,Date &dat)
{
ins<<dat.Year<<"/"<<dat.Month<<"/"<<dat.Day<<endl;
return ins;
}
////////////////////////2//////////////////
#include <iostream>
using namespace std;
class Pairs
{
public:
Pairs();
friend ostream& operator <<(ostream& ins, Pairs& second);
private:
int f; int s;
};
ostream& operator <<(ostream& ins, Pairs &second)
{
ins<<"("<<second.f<<","<<second.s<<")";
return ins;
}
第一个编译能通过,第二个却提示error C2248: 'f' : cannot access private member declared in class 'Pairs'
这是为什么呢?我觉得这两个是一样的啊