英文:
error: no matching function for call to 'Auto::Auto()'
问题
我创建了3个类:Auto(表示"car"),Klant(表示"customer")和AutoVerhuur(表示"car dealership")。
在我的main()函数中,我创建了Auto和Klant对象,并尝试创建一个AutoVerhuur对象。
在这个最后一个类中,我基本上想引用特定的Klant和Auto(哪个客户租了哪辆车)。但是,当我尝试这样做时,出现了错误:
错误:没有匹配的函数来调用‘Auto::Auto()’。
如何正确引用其他对象在我的对象中?
这是我的代码,如果你想看一下:
#include <iostream>
using namespace std;
class Auto {
private:
string type;
double prijs_per_dag;
public:
Auto(string type, double prijs_per_dag) {
this->type = type;
this->prijs_per_dag = prijs_per_dag;
}
void set_prijs_per_dag(double percentage) {
this->prijs_per_dag = percentage;
}
double get_prijs_per_dag() {
return prijs_per_dag;
}
};
class Klant {
private:
string naam;
double korting_percentage;
public:
Klant(string naam):
naam(naam){}
void set_korting(double percentage) {
this->korting_percentage = percentage;
}
double get_korting() {
return this->korting_percentage;
}
string get_name() {
return naam;
}
void set_name(string naam) {
this->naam = naam;
}
};
class AutoHuur {
private:
int aantal_dagen;
Auto wagen;
Klant huur;
public:
AutoHuur(Auto car, Klant huurder, int dagen) {
wagen = car;
huur = huurder;
aantal_dagen = dagen;
}
};
int main() {
Klant k("Mijnheer de Vries");
k.set_korting(10.0);
Auto a1("Peugeot 207", 50);
AutoHuur ah1(a1, k, 4);
}
英文:
I have created 3 classes: Auto (means "car"), Klant (means "customer") and AutoVerhuur (means "car dealership).
In my main(), I have created Auto and Klant objects, and am trying to create an AutoVerhuur object.
In this last class, I basically want to reference to a specifc Klant and Auto (which customer rented which car). But, when I try that, I get an error:
> error: no matching function for call to 'Auto::Auto()'
How do I correctly reference other objects in my object?
Here is my code, if you want to take a look:
#include <iostream>
using namespace std;
class Auto{
private:
string type;
double prijs_per_dag;
public:
Auto(string type, double prijs_per_dag){
this->type = type;
this->prijs_per_dag = prijs_per_dag;
}
void set_prijs_per_dag(double percentage){
this->prijs_per_dag = percentage;
}
double get_prijs_per_dag(){
return prijs_per_dag;
}
};
class Klant{
private:
string naam;
double korting_percentage;
public:
Klant(string naam):
naam(naam){}
void set_korting(double percentage){
this->korting_percentage = percentage;
}
double get_korting(){
return this->korting_percentage;
}
string get_name(){
return naam;
}
void set_name(string naam){
this->naam = naam;
}
};
class AutoHuur{
private:
int aantal_dagen;
Auto wagen;
Klant huur;
public:
AutoHuur(Auto car, Klant huurder, int dagen){
wagen = car;
huur = huurder;
aantal_dagen = dagen;
}
};
int main(){
Klant k("Mijnheer de Vries");
k.set_korting(10.0);
Auto a1("Peugeot 207", 50);
AutoHuur ah1(a1, k, 4);
}
答案1
得分: 3
你的Auto类没有定义默认构造函数,但是你的AutoHuur类有一个Auto wagen;的数据成员,编译器尝试默认构造它(因为你没有告诉它其他方式),因此出现错误。
所以,你需要做以下之一:
-
给
Auto类定义一个默认构造函数,例如:Auto(){ this->type = ""; this->prijs_per_dag = 0; // 或者根据你的需求使用其他默认值... } -
或者,更改
AutoHuur类的构造函数,使用其成员初始化列表来使用所需的Auto构造函数构造wagen成员(对于其他数据成员也应该这样做),例如:AutoHuur(Auto car, Klant huurder, int dagen) : wagen(car), huur(huurder), aantal_dagen(dagen) { }
英文:
Your Auto class does not have a default constructor defined, but your AutoHuur class has an Auto wagen; data member which the compiler is trying to default-construct (because you haven't told it otherwise), hence the error.
So, you need to either:
-
give the
Autoclass a default constructor, eg:Auto(){ this->type = ""; this->prijs_per_dag = 0; // or whatever default values make sense for your needs... } -
Otherwise, change the constructor of the
AutoHuurclass to use its member initialization list to construct thewagenmember using the desiredAutoconstructor (you should do the same for the other data members, too), eg:AutoHuur(Auto car, Klant huurder, int dagen) : wagen(car), huur(huurder), aantal_dagen(dagen) { }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论