C++ class with multiple constructors - Header and cpp file

Asked

Viewed 761 times

2

I have a C++ class where I have two constructors. In the file minhaclasse. h:

public:
  MinhaClasse(int x1);
  MinhaClasse(int x1, int x2);

Then in the.cpp file I create normally the constructors I declared there in the header. So far so good. The problem now happens in my Main class. In the header file of the Main class, I want to declare inside public:

public:
  MinhaClasse classe;

But if I run the project at this point, the line above gives error, saying that there is an ambiguity, because as I did not initialize the class object, and it does not know which constructor to use. The only way I did and it worked was not declare the object inside the Header, and leave to declare it whole already inside the CPP:

 MinhaClasse classe = MinhaClasse(5);

So I come to my question: whether the class MinhaClasse has two builders, has some "trick" for, in my class Main, can declare the 'class' object inside the Header, without initializing it, or in this case I will need to really not create the header object and leave to declare it completely inside the CPP??

File calculator. h

#ifndef CALCULADORA_H
#define CALCULADORA_H
class Calculadora
{
public:
    Calculadora(int x=0);
    Calculadora(int x=0, int y=0);
};
#endif // CALCULADORA_H

File calculator.cpp:

#include "calculadora.h"
#include "qdebug.h"
Calculadora::Calculadora(int x)
{
    qDebug() << x;
}
Calculadora::Calculadora(int x, int y)
{
    qDebug() << x+y;
}

And on the mainwindow. h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "calculadora.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:

private:
    Ui::MainWindow *ui;
    Calculadora calc; // aqui dá o erro!!!!
};

#endif // MAINWINDOW_H

1 answer

2


Instead of "run" the class call directly with:

private:
    Ui::MainWindow *ui;
    Calculadora calc; // aqui dá o erro!!!!

Do so:

private:
    Ui::MainWindow *ui;
    Calculadora *calc;

And on CPP call it that:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    calc = new Calculadora();
}

Read more on:

Error call of overloaded is ambiguous

What happens is that when you try to set default parameters it cannot decide which one to use.

Instead:

public:
    Calculadora(int x=0);
    Calculadora(int x=0, int y=0);

Do this:

public:
    Calculadora(int x=0);
    Calculadora(int x, int y);
  • Now it worked!!!! I will try to better understand the pointers, because for me it is a new concept. I really appreciate your attention and patience!

  • @Pauloluvisoto If the answer solved your problem please mark it as correct by clicking the green button (below the points in the answer) as Correct. Thank you

  • 1

    The answer is correct and very good. But I think it could be improved with some details: (1) As the class Calculadora of the example does not inherit from QObject, the created pointer should be deleted to avoid memory leaks. This will hardly be a problem if this class is created/used only in the main window, but it is at least good to take note of the case. (2) An alternative to not using pointers is to set a default constructor (default) and setters for the parameters. Thus the class can be declared statically and the parameters changed in the main.

  • 1

    @Luizvieira I had talked about this, but I removed it from the answer because the author seems to be starting in learning classes, but I will re-add :) as soon as possible.

  • Got it. Well, the AP has already marked it as accepted, so I think for him it’s been enough help. If you add it, I think it’s a better help for other people as well. :)

  • 1

    Although I have known programming for a considerable time, C++ has brought me some new concepts, which I am having to assimilate. I’ve got my eye on everything you’re talking about to see what else I can absorb.

Show 1 more comment

Browser other questions tagged

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