QDialog explicit constructor without argument - how to use it correctly?

I tested this with a derived class, but it is the same with the QDialog base class:

when i do

QDialog dialog(); dialog.exec(); 

the compiler complains

 J:\...\mainwindow.cpp:-1: In member function 'void MainWindow::on_viewButton_pressed()': J:\...\mainwindow.cpp:72: Fehler:request for member 'exec' in 'dialog', which is of non-class type 'QDialog()' 

This has something to do with the constructor used, because when I do

 QDialog dialog(0); dialog.exec(); 

The code compiles without errors. This also works:

 QDialog *dial = new QDialog(); dial->exec(); 

So. Is this because of an explicit constructor?

The documentation says that it is defined as

 QDialog::QDialog ( QWidget * parent = 0, Qt::WindowFlags f = 0 ) 

So the first two examples should not match? And why does the compiler complain about the second line, and not about the constructor.

Thanks for the enlightenment, we welcome further readings on the topic.

+4
source share
2 answers

QDialog dialog();

This declares a function called dialog that takes nothing and returns a QDialog

If this surprises you, suppose you named your variable f instead of a dialog. What do we get?

QDialog f();

Now it looks more like a function, right? :)

You need

QDialog dialog;

Whenever something can be interpreted as an declaration and something else, the compiler always decides the ambiguity in favor of the declaration

+8
source
 QDialog dialog; 

- The correct syntax for creating a QDialog stack on the stack with the default constructor.

+2
source

All Articles