【问题标题】:Incomplete Type is not Allowed QApplication不完整的类型是不允许的 QApplication
【发布时间】:2021-02-10 19:13:32
【问题描述】:

我正在使用 Qt 学习 C++。我已经安装了 Qt 5.15 并且正在使用 VS2019。我有以下代码(根据我正在阅读的教科书中的示例):

#include <QtGui>

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
    QTextStream cout(stdout);

    //declarations of variables
    int answer = 0;

    do{
        //local variables
        int factArg = 0;
        int fact(1);
        factArg = QInputDialog::getInt(0, "Factorial Calculator", "Factorial of:", 1);
        cout << "User entered: " << factArg << endl;
        int i = 2;
        while (i <= factArg) {
            fact = fact * i;
            ++i;
        }
        QString response = QString("The factorial of %1 is %2.\n%3").arg(factArg).arg(fact).arg("Do you want to compute another factorial?");
        answer = QMessageBox::question(0, "Play again?", response, QMessageBox::Yes | QMessageBox::No);
    } while (answer == QMessageBox::Yes);

    return EXIT_SUCCESS;
}

但是,在将 QApplication 的实例创建为 app 时,我收到以下错误:

Incomplete Type is not Allowed

我还收到了 QInputDialogQMessageBox 类的以下错误:

name followed by '::' must be a class or namespace name

我不确定为什么会发生这种情况 - 可能是带有命名空间的东西,但我不确定要提供什么范围。我在网上搜索过,但无济于事。

更新

添加下面的标头引用会给每个 cannot open source file 错误。

#include <QApplication>
#include <QInputDialog>
#include <QMessageBox>

我还在我的代码中添加了来自 cmets 的建议,现在如下:

#include <QtGui>
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
    QTextStream cout(stdout);

    //declarations of variables
    int answer = 0;

    do{
        //local variables
        int factArg = 0;
        int fact(1);
        factArg = QInputDialog::getInt(0, "Factorial Calculator", "Factorial of:", 1);
        cout << "User entered: " << factArg << endl;
        int i = 2;
        while (i <= factArg) {
            fact = fact * i;
            ++i;
        }
        QString response = QString("The factorial of %1 is %2.\n%3").arg(factArg).arg(fact).arg("Do you want to compute another factorial?");
        answer = QMessageBox::question(0, "Play again?", response, QMessageBox::Yes | QMessageBox::No);
    } while (answer == QMessageBox::Yes);

    return EXIT_SUCCESS;
}

但我仍然收到同样的错误。

【问题讨论】:

  • 编译器可能只看到QApplication 的前向声明。尝试添加#include &lt;QApplication&gt;
  • @G.M.这就是我最初尝试的方法,但是当我添加该引用时,我收到以下错误:cannot open source file
  • #include &lt;QtGui&gt;更改为#include &lt;QtWidgets&gt;
  • @eyllanesc 我得到相同的cannot open source file QtWidgets
  • @Dean 还添加了#include &lt;iostream&gt;using namespace std;

标签: c++ qt


【解决方案1】:

你必须在应用程序中包含一些qt头......这就是消息的含义

您只需将其添加到您的代码中

#include <QApplication>
#include <QInputDialog>
#include <QMessageBox>

【讨论】:

  • 我得到cannot open source file 为每个这些声明..?
【解决方案2】:

要包含的正确标题如下:

#include <QtWidgets/QApplication>
#include <QtWidgets/QInputDialog>
#include <QtWidgets/QMessageBox>

一旦声明了这些,编译器就会接受代码。

【讨论】:

    【解决方案3】:

    我要为你剪切和粘贴一个工作程序。

    Qt 似乎有一堆前向定义。如果我尝试让 IDE 真正帮助我选择方法调用,我可以说我缺少正确的包含文件。简而言之,您几乎必须#include 您将要使用的每个小部件类型或其他类。我没有遇到任何捷径。

    #include <QApplication>
    
    #include "MainWindow.h"
    
    using namespace std;
    
    /**
     * Main entry point.
     */
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        MainWindow mainWindow;
        mainWindow.show();
    
        return app.exec();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-12
      • 1970-01-01
      • 2017-03-29
      • 1970-01-01
      • 2013-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多