#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 07:02:24 浏览: 70
你的代码没有问题,可能是你的编译器版本较老,无法识别 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 支持。
相关问题
#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/20250102104920.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044736.png)