-1

I'm trying to make the constructor for a subclass. But I keep getting this error message. I've tried searching here, but none of the answers I found applied to my problem. Sorry if it's been asked before.

In constructor 'EixoDinamico::EixoDinamico(double, double, Serie*, bool)':
error: no matching function for call to 'Eixo::Eixo()'
note: candidates are: note: Eixo::Eixo(std::string, double, double)
note: candidate expects 3 arguments, 0 provided
note: Eixo::Eixo(const Eixo&)
note: candidate expects 1 argument, 0 provided

EDIT: If I rewrite the code so that the subclass is now a class on its own, the problem disappears, but I need it to be a subclass.

Here are the codes:

Eixo.h

#ifndef EIXO_H
#define EIXO_H
#include <iostream>

using namespace std;

class Eixo
{
public:
    Eixo(string titulo, double minimo, double maximo);
    virtual ~Eixo();

private:
    string titulo;
    double minimo;
    double maximo;
};

#endif // EIXO_H'

Eixo.cpp

#include "Eixo.h"
#include <iostream>

Eixo::Eixo(string titulo, double minimo, double maximo)
{
this->maximo = maximo;
this->minimo = minimo;
this->titulo = titulo;
}

Eixo::~Eixo()
{
    //dtor
}

EixoDinamico.h

#ifndef EIXODINAMICO_H
#define EIXODINAMICO_H
#include "Eixo.h"


class EixoDinamico : public Eixo
{
public:
    EixoDinamico(double minimoPadrao, double maximoPadrao, Serie* 
base, bool orientacaoHorizontal);
    virtual ~EixoDinamico();

private:

};

#endif // EIXODINAMICO_H

EixoDinamico.cpp

#include "EixoDinamico.h"
#include "Eixo.h"
EixoDinamico::EixoDinamico(double minimoPadrao, double maximoPadrao, Serie* 
base, bool orientacaoHorizontal):Eixo()
{
     if(base->getQuantidade()<2){
    inicioEixo = minimoPadrao;
    fimEixo = maximoPadrao;
}
    limiteInferior = base->getLimiteInferior();
    limiteSuperior = base->getLimiteSuperior();
    if (orientacaoHorizontal){
        inicioEixo = limiteInferior->getX();
        fimEixo = limiteSuperior->getX();
}
    else{
    inicioEixo = limiteInferior->getY();
    fimEixo = limiteSuperior->getY();
}
}

EixoDinamico::~EixoDinamico()
{
//dtor
}
  • 2
    Possible duplicate of [What are the rules for calling the superclass constructor?](https://stackoverflow.com/questions/120876/what-are-the-rules-for-calling-the-superclass-constructor) – underscore_d Oct 25 '17 at 18:58
  • 2
    The error is precisely correct and clear. There is no constructor of the base class that accepts an empty argument list, and yet here you are trying to call one. Put another way: what should happen to the base class's members if the subclass didn't get the base to initialise them? Or does the subclass not use/care about those variables? If so, why does it inherit from a class that has them? It shouldn't. Also, why do you declare one signature of constructor in `EixoDinamico.hpp` but define another in `EixoDinamico.cpp`? I think a bit more background reading on how constructors work is in order – underscore_d Oct 25 '17 at 19:00
  • @underscore_d The diferent declaration was a mistake when creating the question, thank you for pointing it out. The subclass uses the `Serie* base` to get the variables needed to inicialise the class constructor. In other words, the variables `(string titulo, double minimo, double maximo)` need to be located inside `Serie* base` by the EixoDinamico constructor. – 121 Gigamatts Oct 25 '17 at 19:08
  • That still means that you should probably hand them over to the constructor of the base class. – Knoep Oct 25 '17 at 19:16
  • What is a `Serie`? And yes, prefer using the ctor init list, not setting members in the ctor body, which in this case means calling the existing non-default constructor of `Eixo`. – underscore_d Oct 25 '17 at 19:19

1 Answers1

1

In the constructor of EixoDinamico you're calling the default constructor of Eixo (Eixo()), but that doesn't exist. The declaration of a custom construct for Eixo disables the automatic generation of a default constructor and you haven't declared one explicitly. To do that, add

Eixo() = default;

to the declaration of Eixo or implement one yourself.

Also make sure that calling the default constructor is really what you want. As underscore_d has pointed out, that doesn't make much sense.

Knoep
  • 858
  • 6
  • 13
  • ...and then really consider why your subclass does not initialise those member variables in its base, and whether the answer to that question means it shouldn't be a subclass or those members should be somewhere else. – underscore_d Oct 25 '17 at 19:06