Problem calling base constructor in inherited constructor in C++

Asked

Viewed 573 times

0

I’m taking data structures in college at C++ and this language is really crazy.

I am trying to call the parent constructor in the constructor of the heirs classes, but the following error appears:

error: no matching Function for call to Vehicle::Vehicle(Std::__cxx11::string&)'

Here comes the code:

veiculo.h

    #ifndef VEICULO_H_
    #define VEICULO_H_
    #include <iostream>

    using namespace std;

    class Veiculo {

    protected:
        string nome;

    public:

        Veiculo(const char *nome) {
            this->nome = string(nome);
            cout << "Criação de Veículo" << nome << endl;
        }

        ~Veiculo(){
            cout << "Destruição de Veículo" << nome << endl;
        }

    };

    class Terrestre : public Veiculo {
    public:

        Terrestre() : Veiculo(nome){
            this->nome = Veiculo::nome;
            cout << "Criação de Terrestre" << nome << endl;
        };

        ~Terrestre() : Veiculo() {
            cout << "Destruição de Terrestre" << nome << endl;
        }
    };

    class Aquatico : public Veiculo {
    public:

        Aquatico() : Veiculo(nome) {
            this->nome = Veiculo::nome;
            cout << "Criação de Aquatico" << nome << endl;
        };

        ~Aquatico() {
            cout << "Destruição de Aquatico" << nome << endl;
        }

    };

    class Aereo : public Veiculo {
    public:

        Aereo() : Veiculo(nome) {
            this->nome = Veiculo::nome;
            cout << "Criação de Aereo" << nome << endl;

        };

        ~Aereo() {
            cout << "Destruição de Aereo" << nome << endl;
        }

    };


    #endif /* VEICULO_H_ */
    `

principal.cpp:

    `
    #include <iostream>
    #include "veiculo.h"

    using namespace std;

    int main() {
        cout << "Segunda pratica de AED em C++" << endl;

        Veiculo v1("v1");
        Terrestre t1("t1");
        Aquatico aq1("aq1");
        Aereo ar1("ar1");
    }

1 answer

0


You are using constructors to receive a string on main:

int main() {
    ...
    Terrestre t1("t1");

But in the class constructor there is no such definition:

Terrestre() : Veiculo(nome)

Is Terrestre() that has no parameters. Instead you want to do as you did in the class Veiculo and add the missing parameter, thus:

Terrestre(const char *nome) : Veiculo(nome){

And has the same problem for the class Aquatico and Aereo.

There is another error, which is in the destroyer of Earthling, here:

~Terrestre() : Veiculo() {

Which is calling the base constructor. Actually the destructor cannot have initializers. In addition, the compiler already generates code to call the class destructors in order, from derivatives to base.

Change it to:

~Terrestre() {

Browser other questions tagged

You are not signed in. Login or sign up in order to post.