c++ - 不完整的类型是不允许的 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;
}
但是,我在创建QApplication
as的实例时收到以下错误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>
我还在我的代码中添加了评论中的建议,现在如下:
#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;
}
但我仍然收到同样的错误。
解决方案
您必须在应用程序中包含一些 qt 标头...这就是消息的含义
您只需要将其添加到您的代码中
#include <QApplication>
#include <QInputDialog>
#include <QMessageBox>
推荐阅读
- python - 读取 numpy 文件(.npy)而不导入 numpy 或任何其他非本地导入
- c# - 将消息头添加到请求
- javascript - VueJS 将对象传递给根组件函数
- css - 如何在 Ionic 4 的中心 fab 按钮上进行设置?
- asp.net-mvc - DropDownListFor 未显示在我的模式中,而我的 selectListItem 不为空
- google-drive-api - 在根目录下删除大量文件
- java - 更改底部导航栏大小
- safari - 即使在创建 onclick 时,Safari AudioContext 也会暂停
- mysql - 在 MySQL 中设计其他进程可以轮询的简单事件表。如何处理交易?
- javascript - 显示来自多个地址的坐标(Google Map API)地理编码