The class created when creating a form in Qt. Why can't I define a function inside a class? With such a record, I get the error invalid use of incomplete type 'class Ui::EnterWindow'

 namespace Ui { class EnterWindow; } class EnterWindow : public QDialog { Q_OBJECT public: explicit EnterWindow(QWidget *parent = 0); ~EnterWindow(); QString getLogin() { return ui->label->text(); } private: Ui::EnterWindow *ui; }; 

If I write the functions in this form

 QString getLogin(); 

And in the .cpp file I will determine, everything will be fine.

    1 answer 1

    Because invalid use of incomplete type 'class Ui::EnterWindow' .

    Those. At this point, the definition of the Ui::EnterWindow not available (a header with it is included later and only in the * .cpp file).

    There are several reasons for not including too much into the headline, and almost all of them are quite small, advisory and subjective (most of them are dictated by the categories of “dirty” and / or “bad practice”), in particular, some of them are:

    1. A fairly common thesis: “relying on the fact that the Foo header includes the Bar (in the Baz module) is a bad practice.” And the inclusion of everything in the head promotes it.
    2. The namespace becomes dirty when compiling other modules.
    3. As a result, the compilation time and the likelihood of errors are significantly increased.
    4. It is not possible to include everything in headers in case of cyclic dependencies.
    5. Providing the user with an interface that should not be of interest to him is also a bad practice.
    • Automatic inclusion of the header 'ui_enterwindow.h' is recorded in the * .cpp file, can its inclusion be transferred to the file 'enterwindow.h'? - Mishakov Maxim
    • @MishakovMaksim, it is possible, but there is no good reason for this, IMHO, but this is still a relatively dirty approach .. - Fat-Zer
    • Do you happen to know why the inclusion of ui in the * .cpp file is connected, and not in * .h? I somehow try to include everything in the header, and in * .cpp only the header, because it seemed to me that it was so “right” - Mishakov Maxim
    • @MishakovMaksim, added to the answer. - Fat-Zer