error: no matching function for call to ‘Auto::Auto()’

huangapple go评论51阅读模式
英文:

error: no matching function for call to 'Auto::Auto()'

问题

我创建了3个类:Auto(表示"car"),Klant(表示"customer")和AutoVerhuur(表示"car dealership")。

在我的main()函数中,我创建了AutoKlant对象,并尝试创建一个AutoVerhuur对象。

在这个最后一个类中,我基本上想引用特定的KlantAuto(哪个客户租了哪辆车)。但是,当我尝试这样做时,出现了错误:

错误:没有匹配的函数来调用‘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 &lt;iostream&gt;

using namespace std;
class Auto{
    private:
    string type;
    double prijs_per_dag;

    public:
    Auto(string type, double prijs_per_dag){
        this-&gt;type = type;
        this-&gt;prijs_per_dag = prijs_per_dag;
    }

    void set_prijs_per_dag(double percentage){
        this-&gt;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-&gt;korting_percentage = percentage;
    }

    double get_korting(){
        return this-&gt;korting_percentage;
    }

    string get_name(){
        return naam;
    }

    void set_name(string naam){
        this-&gt;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(&quot;Mijnheer de Vries&quot;);
    k.set_korting(10.0);

    Auto a1(&quot;Peugeot 207&quot;, 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 Auto class a default constructor, eg:

    Auto(){
        this-&gt;type = &quot;&quot;;
        this-&gt;prijs_per_dag = 0;
        // or whatever default values make sense for your needs...
    }
    
  • Otherwise, change the constructor of the AutoHuur class to use its member initialization list to construct the wagen member using the desired Auto constructor (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)
    {
    }
    

huangapple
  • 本文由 发表于 2023年2月10日 04:26:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定