#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 19:03:00 浏览: 31
在输出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类型成员变量时进行相应的操作。
相关问题
定义类date的带默认值(为2018年7月1日)的函数与复制构造函数 #include <iostream> using namespace std; class date { private: int year,month,day; public: date( 1 ) { year=y; month=m; day=d;} date( 2 ) { year=d1.year; month=d1.month ;day=d1.day ; } }; int main() { date d1(2004,10,8); date d2=d1; return 0; }
可以按照以下方式定义类date的带默认值的构造函数和复制构造函数:
```c++
#include <iostream>
using namespace std;
class date {
private:
int year, month, day;
public:
date(int y=2018, int m=7, int d=1) {
year = y;
month = m;
day = d;
}
date(const date& d1) {
year = d1.year;
month = d1.month;
day = d1.day;
}
};
int main() {
date d1(2004, 10, 8);
date d2 = d1;
return 0;
}
```
在这个例子中,类date的构造函数有三个参数,分别代表年、月、日。在构造函数中,可以使用默认参数来指定默认值。复制构造函数的参数是一个date对象的引用,它将另一个date对象复制到当前对象。在main函数中,我们使用构造函数创建一个日期对象d1,然后使用复制构造函数将其复制到另一个日期对象d2。
#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) {} }; class Cperson { private: string Name; string Num; Date brith; Cperson(const char *pName="张三", 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; } int Getbrith() { return brith; } }; cass Cstudent: private Cperson { private: int StNum; }; class Cteacher:public Cperson { private: Date Time; }; int main() { cout << "手机编程APP"; return 0; }有什么错误
在类Cperson的构造函数中,初始化成员变量时,应该使用初始化列表中的参数,而不是使用类内部定义的变量。正确的构造函数应该是:
```
Cperson(const char *pName="张三",
string Num="123456789123456789",
int year=2000,int month=1,int day=1):
Name(pName),Num(Num),brith(year,month,day)
{}
```
此外,在类Cperson的成员函数Getbrith中,返回值应该是一个Date对象,而不是int类型。应该改为:
```
Date Getbrith()
{
return brith;
}
```
最后,在类Cstudent和Cteacher中,应该声明访问权限为public,否则外部无法访问这些类的成员。修正后的代码如下:
```
#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)
{}
};
class Cperson {
public:
string Name;
string Num;
Date brith;
Cperson(const char *pName="张三",
string Num="123456789123456789",
int year=2000,int month=1,int day=1):
Name(pName),Num(Num),brith(year,month,day)
{}
string GetName()
{
return Name;
}
string GetNum()
{
return Num;
}
Date Getbrith()
{
return brith;
}
};
class Cstudent: public Cperson {
public:
int StNum;
};
class Cteacher:public Cperson {
private:
Date Time;
public:
// 省略构造函数
};
int main() {
cout << "手机编程APP";
return 0;
}
```
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)