0

I am learning C++ with Qt. I have installed Qt 5.15 and am using VS2019. I have the below code (as per an example in a textbook I am working through):

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

However, I am recieving the below error when creating an instance of QApplication as app:

Incomplete Type is not Allowed

I am also recieving the below error for the QInputDialog and QMessageBox classes:

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

I am not sure why this is happening - presumably something with a namespace, but I am not sure what scope to provide. I have searched the web but to no avail.

UPDATE

Adding the below header references give cannot open source file error for each.

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

I have also added suggestions from the comments to my code, now below:

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

But I am still recieving the same errors.

Dean
  • 2,326
  • 3
  • 13
  • 32
  • 1
    The compiler probably sees only a forward declaration for `QApplication`. Try adding `#include ` . – G.M. Feb 10 '21 at 19:15
  • @G.M. that's what I tried initially, but when I add that reference I get the following error: `cannot open source file` – Dean Feb 10 '21 at 19:19
  • change `#include ` to `#include ` – eyllanesc Feb 10 '21 at 19:19
  • @eyllanesc I get the same `cannot open source file QtWidgets` – Dean Feb 10 '21 at 19:21
  • @Dean also add `#include ` and `using namespace std; ` – eyllanesc Feb 10 '21 at 19:25
  • Does this answer your question? [C++ - What does "Incomplete type not allowed" error mean, and how can I fix it?](https://stackoverflow.com/questions/49354611/c-what-does-incomplete-type-not-allowed-error-mean-and-how-can-i-fix-it) – Pat. ANDRIA Feb 10 '21 at 19:26
  • @eyllanesc I have added those but still nothing.. – Dean Feb 10 '21 at 19:31
  • @Dean `"but still nothing"` really doesn't help anyone else understand the problem. Please edit your question to include the latest code (including .pro file if there is one) and the error messages you see verbatim. – G.M. Feb 10 '21 at 19:37
  • @G.M. `but still nothing` implies no change from the original state with the additional code. I will update my post accordingly. – Dean Feb 10 '21 at 19:38
  • Do you have Microsoft Visual Studio installed? It seems to me that the cause of the error is that – eyllanesc Feb 10 '21 at 19:44
  • @eyllanesc I am using Visual Studio 2019? Can I not use VS for Qt applications? – Dean Feb 10 '21 at 20:37

3 Answers3

2

The correct headers to include are the following:

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

Once declaring these, the compiler accepts the code.

Dean
  • 2,326
  • 3
  • 13
  • 32
1

You have to include some qt headers in the app... that is the meaning of the message

you just need to add this to your code

#include <QApplication>
#include <QInputDialog>
#include <QMessageBox>
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

I'm going to cut and paste a working program for you.

Qt seems to have a bunch of forward definitions. I can tell I'm missing the right include file if I try to get the IDE to actually help me select a method call. In short, you pretty much have to #include every widget type or other class you're going to use. I haven't run into any shortcuts.

#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();
}
Joseph Larson
  • 8,530
  • 1
  • 19
  • 36