#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()'。
时间: 2024-02-01 17:02:24 浏览: 63
你的代码没有问题,可能是你的编译器版本较老,无法识别 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 支持。
阅读全文