#include
#include
using namespace std;
class Time
{
private:
int hour;
int minute;
int second;
public:
Time(int hour, int minute, int second)
{
this->hour = hour;
this->minute = minute;
this->second = second;
}
friend class Date;
};
class Date
{
private:
int year;
int month;
int day;
public:
Date(int y, int m, int d, int h, int mi, int s):t(h, mi, s)
{
year = y;
month = m;
day = d;
}
void showDate()
{
cout << "当前日期为:";
cout << year << "-" << month << "-" << day << "\t";
cout << t.hour << "时" << t.minute << "分" << t.second << "秒" << endl;
}
};
int main()
{
Date date(2019, 8, 10, 11, 56, 35);
date.showDate();
return 0;
}
错误:
1>c:\users\发\source\repos\project9\project9\源1.cpp(28): error C2614: “Date”: 非法的成员初始化:“t”不是基或成员
1>c:\users\发\source\repos\project9\project9\源1.cpp(38): error C2065: “t”: 未声明的标识符
1>已完成生成项目“Project9.vcxproj”的操作 - 失败。
在VS2017中运行
目的是输出设定值
cout << t.hour << "时" << t.minute << "分" << t.second << "秒" << endl;
改成
cout << this->hour << "时" << this->minute << "分" << this->second << "秒" << endl;