#include<iostream>
using namespace std;
class Date;
class Time
{public:
Time(int,int,int);
friend void Date::show(Time &);
private:
int hour;
int minute;
int sec;
};
class Date
{public:
Date(int,int,int);
void show(Time &);
private:
int month;
int day;
int year;
};
Time::Time(int h,int m,int s)
{hour=h;
minute=m;
sec=s;
}
void Date::show(Time &d)
{cout<<d.hour<<"/"<<d.minute<<d.sec<<end1;//出错位置
cout<<month<<":"<<day<<":"<<year<<endl;
}
Date::Date(int m,int d,int y)
{month=m;
day=d;
year=y;
}
int main()
{Time t1(10,13,56);
Date d1(12,25,2004);
t1.display(d1);
return 0;
}
私有变量不能这么访问啊
把Date类设置为Time类的友元类,其它地方也有修改需要,参考如下:
#include<iostream>
using namespace std;
class Date;
class Time
{
friend Date;
public:
Time(int,int,int);
friend void Date::show(Time &);
private:
int hour;
int minute;
int sec;
};
class Date
{public:
Date(int,int,int);
void show(Time &);
private:
int month;
int day;
int year;
};
Time::Time(int h,int m,int s)
{hour=h;
minute=m;
sec=s;
}
void Date::show(Time &d)
{cout<<d.hour<<"/"<<d.minute<<d.sec<<endl;//出错位置
cout<<month<<":"<<day<<":"<<year<<endl;
}
Date::Date(int m,int d,int y)
{month=m;
day=d;
year=y;
}
int main()
{Time t1(10,13,56);
Date d1(12,25,2004);
d1.show(t1);
return 0;
}