| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1269 人关注过本帖
标题:[求助]输出流重载问题?
取消只看楼主 加入收藏
bill8888
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2007-3-10
收藏
 问题点数:0 回复次数:1 
[求助]输出流重载问题?

程序是这样的:
#include<iostream>
#include<iomanip>
using namespace std;

class Date{
int year, month, day;
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& h);
};
Date::Date(const string& s){
year = atoi(s.substr(0,4).c_str());
month = atoi(s.substr(5,2).c_str());
day = atoi(s.substr(8,2).c_str());
}
Date::Date(int y, int m, int d){ year=y,month=m,day=d; }
bool Date::isLeapYear()const{
return (year % 4==0 && year % 100 )|| year % 400==0;
}
ostream& operator<<(ostream& o, const Date& h)
{
o<<setfill('0')<<setw(4)<<h.year<<'-'<<setw(2)<<h.month<<'-'
<<setw(2)<<h.day<<'\n'<<setfill(' ');
return o;
}
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;
return 0;
}

错误是:
--------------------Configuration: sadf - Win32 Debug--------------------
Compiling...
sadf.cpp
F:\学习资料\编程\vc++\cpp文件\sadf.cpp(32) : error C2248: 'year' : cannot access private member declared in class 'Date'
F:\学习资料\编程\vc++\cpp文件\sadf.cpp(10) : see declaration of 'year'
F:\学习资料\编程\vc++\cpp文件\sadf.cpp(32) : error C2248: 'month' : cannot access private member declared in class 'Date'
F:\学习资料\编程\vc++\cpp文件\sadf.cpp(10) : see declaration of 'month'
F:\学习资料\编程\vc++\cpp文件\sadf.cpp(33) : error C2248: 'day' : cannot access private member declared in class 'Date'
F:\学习资料\编程\vc++\cpp文件\sadf.cpp(10) : see declaration of 'day'
F:\学习资料\编程\vc++\cpp文件\sadf.cpp(42) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.

sadf.obj - 4 error(s), 0 warning(s)


搞不明白,为什么说不能访类里的私有变量,明明可以的,这怎么解释啊?


[此贴子已经被作者于2007-4-29 23:38:03编辑过]

搜索更多相关主题的帖子: Date int const str 
2007-04-29 23:37
bill8888
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2007-3-10
收藏
得分:0 

我在DEV C++里运行能够通过
真不知道VC++是怎么回事啊
居然友元函数不能访问类里的私有变量,有违标准C++的规定嘛


2007-04-30 00:35
快速回复:[求助]输出流重载问题?
数据加载中...
 
   



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

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