#include <iostream> using namespace std; class Date { public: int year,month,day; Date(int year=2000,int month=1,int day=1):year(year),month(month),day(day) {} void print() { cout<<year<<","<<month<<","<<day; } }; class person { public: string Name; string Num; Date brith; person(string Name="张三", string Num="123456789123456789", int year=2000,int month=1,int day=1): Name(Name),Num(Num),brith(year,month,day) {} string GetName() { return Name; } string GetNum() { return Num; } Date Getbrith() { return brith; } }; class student: private person { private: int StNum; }; class teacher:public person { private: Date Time; }; class CTA :public student,public teacher { private: int No; }; int main() { person p1("张三","123456789123456789",2000,1,1); cout<<"序号"<<endl; cout<<" 1 "<<endl; cout<<" "<<p1.GetName()<<endl; cout<<" "<<p1.GetNum()<<endl; cout<<" "<<p1.Getbrith()<<endl; person p2("李四","123456789123456789",2001,1,1); cout<<" 2 "<<endl; cout<<" "<<p2.GetName()<<endl; cout<<" "<<p2.GetNum()<<endl; cout<<" "<<p2.Getbrith()<<endl; return 0; }no match for 'operator' and 'Date') cout怎么解决
时间: 2024-01-25 17:03:00 浏览: 134
使用include <iostream>时一定要加using namespace std
在输出person类中的Date类型成员变量时,需要重载输出运算符<<,否则会出现no match for 'operator' and 'Date'的错误。可以在Date类中添加如下重载函数:
```
friend ostream& operator<<(ostream& os, const Date& date)
{
os << date.year << "," << date.month << "," << date.day;
return os;
}
```
然后在输出person类中的Date类型成员变量时,可以使用cout<<来调用该重载函数:
```
cout << " " << p1.Getbrith() << endl;
```
同样的,也需要在输出teacher类中的Date类型成员变量时进行相应的操作。
阅读全文