| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 587 人关注过本帖
标题:求这个程序友元函数为什么访问不了私有成员?<<ambigurous 如何改?
只看楼主 加入收藏
韩雨航
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2012-1-12
结帖率:57.14%
收藏
已结贴  问题点数:10 回复次数:1 
求这个程序友元函数为什么访问不了私有成员?<<ambigurous 如何改?
构造函数重载.cpp
E:\程序\构造函数重载.cpp(33) : error C2248: 'year' : cannot access private member declared in class 'Date'
        E:\程序\构造函数重载.cpp(8) : see declaration of 'year'
E:\程序\构造函数重载.cpp(33) : error C2248: 'month' : cannot access private member declared in class 'Date'
        E:\程序\构造函数重载.cpp(8) : see declaration of 'month'
E:\程序\构造函数重载.cpp(34) : error C2248: 'date' : cannot access private member declared in class 'Date'
        E:\程序\构造函数重载.cpp(8) : see declaration of 'date'
E:\程序\构造函数重载.cpp(43) : error C2593: 'operator <<' is ambiguous
E:\程序\构造函数重载.cpp(44) : warning C4508: 'main' : function should return a value; 'void' return type assumed
执行 cl.exe 时出错.

构造函数重载.obj - 1 error(s), 0 warning(s)
#include<iostream>                                
#include<iomanip>
#include<string>
using namespace std;
//--------------------
class Date
{
    int year,month,date;
public:
    Date(int y=2000,int m=1,int d=1);                //设置默认参数
    Date(const string &s);                            //重载
    bool isleapyear()const;
    friend ostream& operator<<(ostream& o,const Date& d);
};
Date::Date(int y,int m,int d)
{
    year=y;
    month=m;
    date=d;
}
Date::Date(const string &s)
{
    year=atoi(s.substr(0,4).c_str());
    month=atoi(s.substr(5,2).c_str());
    date=atoi(s.substr(8,2).c_str());
}
bool Date::isleapyear()const
{
    return (year%4==0)&&(year%100)||(year%400==0);
}
ostream& operator<<(ostream& o,const Date& d)
{
     o<<setfill('0')<<setw(4)<<d.year<<"-"<<setfill('0')<<setw(2)<<d.month<<"-";
    return    o<<setfill('0')<<setw(2)<<d.date<<endl;            //??????
}
int main()
{
    Date c("2005-12-28");
    Date d(2003,12,6);
    Date e(2002);                        //默认两个参数
    Date f(2002,12);                    //默认一个参数
    Date g;                                //默认三个参数   
    cout<<c<<d<<e<<f<<g;
}
搜索更多相关主题的帖子: cannot private declared access 函数 
2012-02-14 09:26
mayuebo
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:6
帖 子:257
专家分:1282
注 册:2005-9-8
收藏
得分:7 
报错的信息说得很清楚呀.是那几个变量没有存贮出错.int year,month,date;
这几个没有进行作用域定义,默认的是private,只能在子类中进行读写

成功贵在坚持
2012-02-16 14:03
快速回复:求这个程序友元函数为什么访问不了私有成员?<<ambigurous 如何改? ...
数据加载中...
 
   



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

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