求这个程序友元函数为什么访问不了私有成员?<<ambigurous 如何改?
构造函数重载.cppE:\程序\构造函数重载.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;
}