#include <iostream> #include <string> 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="123456789", 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("张三","123456789",2000,1,1); person p2("李四","123456789",2001,1,1); cout<<"序号"<<endl; cout<<" 1 "<<endl; cout<<" "<<p1.GetName()<<endl; cout<<" "<<p1.GetNum()<<endl; cout<<" "<<p1.Getbrith()<<endl; cout<<" 2 "<<endl; cout<<" "<<p2.GetName()<<endl; cout<<" "<<p2.GetNum()<<endl; cout<<" "<<p2.Getbrith()<<endl; }error: no match for 'operator' and 'Date') cout怎么解决
时间: 2024-03-09 21:45:24 浏览: 120
在输出日期时,cout不知道如何输出Date类型的数据,因此需要重载<<运算符。在Date类中添加如下代码即可:
```
friend ostream& operator<<(ostream& os, const Date& date) {
os << date.year << "-" << date.month << "-" << date.day;
return os;
}
```
然后在输出日期时,使用cout << p1.Getbrith()代替cout << p1.Getbrith()。
相关问题
请设计日期 DATE 类。它包含三个私有数据成员:year (年)、month (月) 和 day (天),均为整数。 #include <iostream> using namespace std
为了设计DATE类,我们可以创建一个名为DATE的结构体或者类,包含year、month和day三个私有成员变量。这里以类的形式展示,因为类更便于封装和操作:
```cpp
class DATE {
private:
int year;
int month;
int day;
public:
// 构造函数,用于初始化日期
DATE(int y = 0, int m = 0, int d = 0) : year(y), month(m), day(d) {}
// 获取和设置年份
int getYear() const { return year; }
void setYear(int new_year) { year = new_year; }
// 获取和设置月份
int getMonth() const { return month; }
void setMonth(int new_month) { month = new_month; }
// 获取和设置日期
int getDay() const { return day; }
void setDay(int new_day) { day = new_day; }
// 其他实用方法,如验证日期是否合法等
// 指定格式打印日期
void printDate() const {
cout << "Date: " << year << "/" << month << "/" << day << endl;
}
// 根据输入字符串解析日期
bool parseString(const string& date_str) {
char delimiter = '/';
size_t pos = 0;
if (!(pos = date_str.find(delimiter))) {
return false;
}
int parsed_year = stoi(date_str.substr(0, pos));
if (parsed_year < 1 || parsed_year > 9999) {
return false;
}
pos += delimiter == '-' ? 1 : 2; // Adjust for possible - instead of /
int parsed_month = stoi(date_str.substr(pos, pos + 2));
if (parsed_month < 1 || parsed_month > 12) {
return false;
}
pos += 2;
int parsed_day = stoi(date_str.substr(pos));
if (parsed_day < 1 || parsed_day > 31) {
return false;
}
setYear(parsed_year);
setMonth(parsed_month);
setDay(parsed_day);
return true;
}
// 其他可能的日期运算方法,例如比较、加减日期等
};
```
#include<iostream>#include<string>using namespace std;class Book;class Date{ private: int year; int month; int day; public: Date(int year_, int month_, int day_); Date(Date &q); friend class Book;};class Book{ private: string name; string author; string isbn; double price; Date date; public: Book(string name_, string author_, string isbn_, Date date_, double price_); Book(Book &p); void printInfo(); };Date::Date(int year_, int month_, int day_):year(year_),month(month_),day(day_){}Date::Date(Date &q){ year=q.year; month=q.month; day=q.day;}Book::Book(string name_, string author_, string isbn_, Date date_, double price_):name(name_),author(author_),isbn(isbn_),date(date_),price(price_){ }Book::Book(Book &p){ name=p.name; author=p.author; isbn=p.isbn; price=p.price; date=p.date;}void Book::printInfo(){ cout<<name<<" "<<author<<" "<<isbn<<" "<<price<<endl; cout<<date.year<<"/"<<date.month<<"/"<<date.day<<endl;}int main(){ int year,month,day; string name,author,isbn; double price; cin>>year>>month>>day; Date date(year,month,day); cin>>name>>author>>isbn>>price; Book b(name,author,isbn,date,price); b.printInfo(); return 0;}这个代码为什么有[错误] no matching 函数 对于 call 到 'Date::Date()'。
你的代码没有问题,可能是你的编译器版本较老,无法识别 C++11 的构造函数语法。你可以尝试将 `Date` 的构造函数改为传统的形式:
```c++
Date::Date(int year_, int month_, int day_)
{
year = year_;
month = month_;
day = day_;
}
Date::Date(const Date& q)
{
year = q.year;
month = q.month;
day = q.day;
}
```
或者在编译时添加 `-std=c++11` 参数启用 C++11 支持。
阅读全文