标题:[求助]友元函数不能访问私有成员
取消只看楼主
zcx8532
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2005-11-9
 问题点数:0 回复次数:0 
[求助]友元函数不能访问私有成员

两个程序:
#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'
这是为什么呢?我觉得这两个是一样的啊

搜索更多相关主题的帖子: 函数 ins ostream dat 
2005-11-13 12:53



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-33679-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.380208 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved