C++ 没有调用 [class] 的匹配函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18971355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No matching function for call to [class]
提问by user1766728
So, I have some classes that I was defining, and it gave me errors for this:
所以,我有一些我正在定义的类,它给了我错误:
#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;
}
The error was:
错误是:
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
I have no idea what is wrong! What does 'no matching function for call' mean?
我不知道出了什么问题!“没有匹配的呼叫功能”是什么意思?
Sorry if this is a noobie question... I just started c++.
对不起,如果这是一个菜鸟问题......我刚开始使用 C++。
回答by Aesthete
This is because you've tried to copy an object and there is no default constructor supplied.
这是因为您尝试复制对象并且没有提供默认构造函数。
this->date = date;
What you should be doing, is initializing everything in the initializer list. There is also no reason you shouldn't be passing some of these arguments by reference.
您应该做的是初始化初始化列表中的所有内容。您也没有理由不通过引用传递其中一些参数。
Payment(int amount, const string& name, const Date& date)
: amount(amount)
, name(name)
, date(date)
{}
Same goes for your Date
class. This will use the compiler generated copy constructor. Be aware that if your class contains more then POD types, you might want to implement your own copy constructor.
你的Date
班级也一样。这将使用编译器生成的复制构造函数。请注意,如果您的类包含更多 POD 类型,您可能需要实现自己的复制构造函数。
回答by Mike Makuch
Because you have
因为你有
Date date;
in your Payment class, you must have a Date constructor that takes no arguments, i.e. Date::Date(), which you do not have specified.
在您的 Payment 类中,您必须有一个不带参数的 Date 构造函数,即您没有指定的 Date::Date()。
回答by gigaplex
The Payment class has a Date child. The Payment constructor is first trying to instantiate the Date child with the default constructor, then later assigning a new value to this child. The problem is that the Date child does not have a default constructor Date::Date(). Either give the Date class a default constructor or change the Payment constructor syntax as follows:
Payment 类有一个 Date 子类。Payment 构造函数首先尝试使用默认构造函数实例化 Date 子项,然后为该子项分配一个新值。问题是 Date 子级没有默认构造函数 Date::Date()。要么为 Date 类提供一个默认构造函数,要么按如下方式更改 Payment 构造函数语法:
Payment::Payment(int amount_, string name_, Date date_) : amount(amount_),
name(name_), date(date_)
{
}
Edit: Aesthete beat me to it
编辑:Esthete 打败了我