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
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!
– Paulo Luvisoto
@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
– Guilherme Nascimento
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 fromQObject
, 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 themain
.– Luiz Vieira
@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.
– Guilherme Nascimento
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. :)
– Luiz Vieira
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.
– Paulo Luvisoto