#include <iostream>
using namespace std;
class Time;
class Date
{
public:
Date(int y=2004,int m=1,int d=1):year(y),month(m),day(d){}
friend void shouDateTime(Date &d,Time &t);
private:
int year,month,day;
};
class Time
{
public:
Time(int h=0,int m=0,int s=0):hour(h),minute(m),second(s){}
friend void showDateTime(Date &d,Time &t);
private:
int hour,minute,second;
};
void showDateTime(Date &d,Time &t)
{
cout<<"Now is"<<d.year<<".";
if(d.month<10) cout<<"0";
cout<<d.month<<".";
if(d.day<10) cout<<"0";
cout<<d.day<<" ";
if(t.hour<10) cout<<"0";
cout<<t.hour<<":";
if(t.minute<10) cout<<"0";
cout<<t.minute<<":";
if(t.second<10) cout<<"0";
cout<<t.second<<"."<<endl;
}
int main()
{
Date d(2004,9,18);
Time t(10,5,30);
showDateTime(d,t);
return 0;
}
每次编译的时候总是出现如下的错误:
…………:error C2248: 'year' : cannot access private member declared in class 'Date'
…………:see declaration of 'year'
请教一下是什么原因呢?谢谢!
[此贴子已经被作者于2005-12-7 22:46:21编辑过]