gpt4 book ai didi

c++ - 没有匹配的函数来调用 [class]

转载 作者:搜寻专家 更新时间:2023-10-31 01:07:44 24 4
gpt4 key购买 nike

所以,我定义了一些类,但它给了我错误:

#include <iostream>
using namespace std;;

//void makepayment(int amount, string name, Date date);
//void listpayments();

class Date;

class Date {
public:
int month;
int day;
int year;
Date(int month, int day, int year) {
this->month = month;
this->day = day;
this->year = year;
}
};

class Payment {
public:
int amount;
string name;
Date date;
Payment(int amount, string name, Date date) {
this->amount = amount;
this->name = name;
this->date = date;
}
};

int main() {
cout <<
"|~~~~~~~~~~~~~~~~~~~~~~~~| \n" <<
"| WELCOME TO THE | \n" <<
"| WESSLES BANK | \n" <<
"| MANAGEMENT SYSTEM! | \n" <<
"|~~~~~~~~~~~~~~~~~~~~~~~~| \n";

for(;;) {

}

return 0;
}

错误是:

foo.cpp: In constructor ‘Payment::Payment(int, std::string, Date)’:
foo.cpp:26:49: error: no matching function for call to ‘Date::Date()’
foo.cpp:26:49: note: candidates are:
foo.cpp:14:5: note: Date::Date(int, int, int)
foo.cpp:14:5: note: candidate expects 3 arguments, 0 provided
foo.cpp:9:7: note: Date::Date(const Date&)
foo.cpp:9:7: note: candidate expects 1 argument, 0 provided

不知道怎么回事! “没有匹配的调用函数”是什么意思?

抱歉,如果这是一个新手问题...我刚开始使用 C++。

最佳答案

这是因为您试图复制一个对象,但没有提供默认构造函数。

this->date = date;

您应该做的是初始化初始化程序列表 中的所有内容。您也没有理由不通过引用传递其中一些参数。

Payment(int amount, const string& name, const Date& date)
: amount(amount)
, name(name)
, date(date)
{}

您的 Date 类也是如此。这将使用编译器生成的复制构造函数。请注意,如果您的类包含的 POD 类型更多,您可能希望实现自己的复制构造函数。

关于c++ - 没有匹配的函数来调用 [class],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18971355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com