QDialog 不带参数的显式构造函数 - 如何正确使用?
我在派生类中经历过这种情况,但与 QDialog 基类相同:
当我这样做时,
QDialog dialog();
dialog.exec();
编译器抱怨
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()'
这与使用的构造函数有关,因为当我这样做时,
QDialog dialog(0);
dialog.exec();
代码编译没有错误。 这也有效:
QDialog *dial = new QDialog();
dial->exec();
所以。是因为显式构造函数吗?
文档说它被定义为
QDialog::QDialog ( QWidget * parent = 0, Qt::WindowFlags f = 0 )
那么前两个示例不应该完全相同吗?为什么编译器在第二行抱怨,而不是在构造函数的那一行。
感谢您的启发,非常欢迎进一步阅读该主题的提示
I experienced this with a derived class, but it's the same with 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 being used, because when i do
QDialog dialog(0);
dialog.exec();
the code compiles without errors.
This is also working:
QDialog *dial = new QDialog();
dial->exec();
So. Is it because of an explicit constructor?
Documentation says it's defined as
QDialog::QDialog ( QWidget * parent = 0, Qt::WindowFlags f = 0 )
So shouldn't the first two examples do exactly the same? And why is the compiler complaining on the second line, not the one with the constructor.
Thanks for enlightenment, hints to further readings on the topic very welcome
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
QDialogdialog();
这声明了一个名为dialog的函数,它不执行任何操作并返回一个
QDialog
如果这让您感到惊讶,假设您将变量命名为f而不是dialog。我们得到什么?
QDialog f();
现在看起来更像是一个函数,不是吗? :)
你需要
QDialog dial;
总是,当某些东西可以被解释为声明而其他东西时,编译器总是会解决有利于声明的歧义
QDialog dialog();
This declares a function named dialog that takes nothing and returns a
QDialog
If that surprises you, suppose you named your variable f instead of dialog. What we get?
QDialog f();
Looks more like a function now, doesn't it? :)
You need
QDialog dialog;
Always, when something can be interpreted as a declaration and something else, the compiler always solves the ambiguity in favor of a declaration
是使用默认构造函数在堆栈上创建
QDialog
的正确语法。is the correct syntax to create a
QDialog
on stack with the default constructor.