主题:通过友元函数实现输出日期和时间(代码短)
我实在检查不出错误,请帮帮忙,谢谢!
按ctrl+F7后显示的结果附在代码后面。
#include<iostream>
using namespace std;
class Date; //对date类的提前引用声明
class Time
{
public:
Time(int h=10,int m=30,int s=0):hour(h),minute(m),second(s){}
void display(Date &);
private:
int hour;
int minute;
int second;
};
class Date
{
public:
Date(int y=1000,int mon=1,int d=1):year(y),month(mon),day(d){}
friend void Time::display(Date &);
private:
int year;
int month;
int day;
};
void Time::dispaly(Date &t)
{
cout<<t.year<<"/"<<t.month<<"/"<<t.day<<endl;
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
int main()
{
Time t1(12,30,0);
Date dat(2010,12,20);
t1.display(dat);
return 0;
}
显示结果如下:
Compiling...
友元成员函数.cpp
E:\C++代码\友元成员函数.cpp(24) : error C2039: 'dispaly' : is not a member of 'Time'
E:\C++代码\友元成员函数.cpp(5) : see declaration of 'Time'
E:\C++代码\友元成员函数.cpp(26) : error C2248: 'year' : cannot access private member declared in class 'Date'
E:\C++代码\友元成员函数.cpp(20) : see declaration of 'year'
E:\C++代码\友元成员函数.cpp(26) : error C2248: 'month' : cannot access private member declared in class 'Date'
E:\C++代码\友元成员函数.cpp(21) : see declaration of 'month'
E:\C++代码\友元成员函数.cpp(26) : error C2248: 'day' : cannot access private member declared in class 'Date'
E:\C++代码\友元成员函数.cpp(22) : see declaration of 'day'
E:\C++代码\友元成员函数.cpp(27) : error C2065: 'hour' : undeclared identifier
E:\C++代码\友元成员函数.cpp(27) : error C2065: 'minute' : undeclared identifier
E:\C++代码\友元成员函数.cpp(27) : error C2065: 'second' : undeclared identifier
Error executing cl.exe.
友元成员函数.obj - 7 error(s), 0 warning(s)
按ctrl+F7后显示的结果附在代码后面。
#include<iostream>
using namespace std;
class Date; //对date类的提前引用声明
class Time
{
public:
Time(int h=10,int m=30,int s=0):hour(h),minute(m),second(s){}
void display(Date &);
private:
int hour;
int minute;
int second;
};
class Date
{
public:
Date(int y=1000,int mon=1,int d=1):year(y),month(mon),day(d){}
friend void Time::display(Date &);
private:
int year;
int month;
int day;
};
void Time::dispaly(Date &t)
{
cout<<t.year<<"/"<<t.month<<"/"<<t.day<<endl;
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
int main()
{
Time t1(12,30,0);
Date dat(2010,12,20);
t1.display(dat);
return 0;
}
显示结果如下:
Compiling...
友元成员函数.cpp
E:\C++代码\友元成员函数.cpp(24) : error C2039: 'dispaly' : is not a member of 'Time'
E:\C++代码\友元成员函数.cpp(5) : see declaration of 'Time'
E:\C++代码\友元成员函数.cpp(26) : error C2248: 'year' : cannot access private member declared in class 'Date'
E:\C++代码\友元成员函数.cpp(20) : see declaration of 'year'
E:\C++代码\友元成员函数.cpp(26) : error C2248: 'month' : cannot access private member declared in class 'Date'
E:\C++代码\友元成员函数.cpp(21) : see declaration of 'month'
E:\C++代码\友元成员函数.cpp(26) : error C2248: 'day' : cannot access private member declared in class 'Date'
E:\C++代码\友元成员函数.cpp(22) : see declaration of 'day'
E:\C++代码\友元成员函数.cpp(27) : error C2065: 'hour' : undeclared identifier
E:\C++代码\友元成员函数.cpp(27) : error C2065: 'minute' : undeclared identifier
E:\C++代码\友元成员函数.cpp(27) : error C2065: 'second' : undeclared identifier
Error executing cl.exe.
友元成员函数.obj - 7 error(s), 0 warning(s)