【发布时间】: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
我还收到了 QInputDialog 和 QMessageBox 类的以下错误:
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 <QApplication>。 -
@G.M.这就是我最初尝试的方法,但是当我添加该引用时,我收到以下错误:
cannot open source file -
将
#include <QtGui>更改为#include <QtWidgets> -
@eyllanesc 我得到相同的
cannot open source file QtWidgets -
@Dean 还添加了
#include <iostream>和using namespace std;