首页 > 解决方案 > 不完整的类型是不允许的 QApplication

问题描述

我正在用 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;
}

但是,我在创建QApplicationas的实例时收到以下错误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>

我还在我的代码中添加了评论中的建议,现在如下:

#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;
}

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

标签: c++qt

解决方案


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

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

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

推荐阅读