Problem in calling base constructor in inherited constructor in C ++

0

I'm getting data structures in college in C ++ and this language is pretty crazy.

I'm trying to call the constructor of the parent class in the heiress class constructor, but the following error appears:

  

error: no matching function for call to Vehicle :: Vehicle (std :: __ cxx11 :: string &) '

Here's 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");
    }
    
asked by anonymous 10.09.2017 / 23:10

1 answer

0

You are using constructors to receive a string in main :

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

But in the class constructor there is no such definition:

Terrestre() : Veiculo(nome)

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

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

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

There is another error, which is in the Terrestrial destructor, here:

~Terrestre() : Veiculo() {

You are calling the base constructor. Actually the destructor can not have initializers. In addition, the compiler already generates code to call class destructors in order, from derivatives to base.

Change it to:

~Terrestre() {
    
10.09.2017 / 23:34