Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Tons of error for no obvious reason



  • Two hours ago, everything was going well, until I realised I needed the same instance of my helper class in my MainWindow than in my LoginWindow. Trying to make this work, I added a Helper parameter to both constructors and passed the same instance to these. However, at some point pressing build gave me 30+ errors. I have gone back to what I started pretty much everywhere, but the errors were still there. I've only changed the main class, which I have reverted to exactly the same, the LoginWindow and MainWindow cpp and h, but the cpp all I changed was the function signature and the this->helper = helper; part. I have also reverted the cpp as far as I know. The LoginWindow header is where every error is popping, but I have literally no clue what they mean. or even if they make sense here. I don't think I've ever hard to make such a post on my programming career, but after having spent two hours on this, I had to write this. Here's a screenshot of it, it shows every short error and the entire LoginWindow (also very short).

    Alt-link: https://imgur.com/a/TnAZ560

    Please help me out, while in a way I'm starting to feel like it's something stupid, I also fear that I broke the entire thing without having a backup. Never again am I thinking "I don't need git for this".



  • @RekTekk249 said in Tons of error for no obvious reason:

    However, at some point pressing build gave me 30+ errors

    Please post the text of code and errors produced, not a screen capture.

    I have gone back to what I started pretty much everywhere, but the errors were still there.

    If you are using a build system builder such as qmake or cmake, it may be necessary to explicitly re-run it or force a clean build.

    Never again am I thinking "I don't need git for this".

    You're definitely not the first to fall into this trap.



  • @jeremy_k I have already tried to re-run qmake or clean the project.
    Sorry about that, here is the stacktrace. https://pastebin.com/NqhkFY1b


  • Lifetime Qt Champion

    Apart from the fact that there is no stacktrace but a compiler warning you should take a look at the file where the compiler generates the warnings for.



  • Is there more than the compile output and issues tab? Sorry, I'm new to qt and c++, where I come from the entire error obviously tells me where the issue is and what is causing it. I have checked the entire file it's sending me to, it's only a 30 line header file... It has not error that I can tell, nothing is underlined, the errors don't tell me anything and I can't seem to find out what's wrong.


  • Qt Champions 2017

    You need to fix your form:

    ..\project4\mainwindow.ui: Warning: The name 'horizontalLayout' (QHBoxLayout) is already in use, defaulting to 'horizontalLayout1'.
    ..\project4\mainwindow.ui: Warning: The name 'horizontalLayout' (QHBoxLayout) is already in use, defaulting to 'horizontalLayout2'.

    means you have multiple objects with the same name.

    Aside from that this:

    C:\Users\etien\Documents\project4\loginwindow.h(22): error C2061: syntax error: identifier 'MainWindow'

    almost certainly means that you've not declared the MainWindow class at the point of usage. You need to either include the full declaration or forward declare the name. The compiler couldn't possibly know what MainWindow is supposed to mean otherwise.

    PS. As @Christian-Ehrlicher noted, the compiler tells you exactly where the problem was encountered - loginwindow.h at line 22.



  • @kshegunov Alright, I'm starting to feel dumb here.

    In my main.cpp. I have:

    QApplication a(argc, argv);
    MainWindow *mw = new MainWindow();
    Helper *h = new Helper();
    LoginWindow *lw = new LoginWindow(mw, h);
    lw->setWindowFlag(Qt::MSWindowsFixedSizeDialogHint, true); /*No Resize*/
    lw->show();
    return a.exec();
    

    What do you mean by MainWindow not being declared before? While I understand that creating an object isn't a class declaration, if it can create that MainWindow instance just fine, why would it have any problems with the LoginWindow constructor?
    In that constructor, I use:

    LoginWindow::LoginWindow(MainWindow *mw, Helper *h, QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::LoginWindow)
    {
        ui->setupUi(this);
        this->mainWindow = mw;
        this->helper = h;
        ....
    }
    

    It might be that doing it like that is not the best way, but this is what I came out with to keep the same instance of helper in both forms.

    Like, I see that line where the compiler tells me the error is, but that constructor has remained the same for the entirety of the project, why would it start telling me this now?

    As for the form warning, thanks for the insight, I didn't know there were warnings hidden in compile output that didn't make their way to issues.


  • Lifetime Qt Champion

    Now two people told you where to look (in mainwindow.h) you still ignore us... I do the same with you. Good luck.


  • Qt Champions 2017

    @RekTekk249 said in Tons of error for no obvious reason:

    What do you mean by MainWindow not being declared before?

    This is related to how C++ is compiled, which is in 'pieces'. A .cpp file (conditionally, but the finer details are unimportant here) defines a translation unit. The division of headers and sources is just semantics for us - people. The point is that when the compiler starts to build the translation unit, at each point a name is used, that name (a.k.a. identifier) has to be defined in some fashion. In your case, when the compiler compiles the translation units, it sees MainWindow, but it doesn't know what this name means. Is it a class, is it a variable of some type, an enum, a constant or whatever? That's why it complains, it needs to know before that. Consider this:

    int main()
    {
        SomeIdentifier * a; //< What is `SomeIdentifier`? Is it `int`, is it a class, is it something else, the compiler can't know at this point
        return 0;
    }
    

    Changing it to:

    class SomeIdentifier;
    int main()
    {
        SomeIdentifier * a; //< Ah, okay, the compiler now knows that this name is the name of a class
        return 0;
    }
    

    will compile fine.

    So in your file, the constructor in question references MainWindow, but the compiler doesn't know what MainWindow is.



  • @kshegunov I get this example, but I don't get how it applies to multiple files/classes. How is one declared before the other is read? I guess that means LoginWindow is "read" before MainWindow?

    On a side note, I've copied the EXACT same code, ui, and .pro file to another project and suddently...... it works.......
    I have literally no clue how that could be, but the only file I haven't copied over was .pro.user file.

    @Christian-Ehrlicher I am sorry if that's how you took it. I was so focused on the line the error was that I hadn't thought of looking in the actual class. But if copying the project over fixed it, does that mean I wasn't so dumb after all, or it maybe it was a simple thing that someone more experienced would have spent 5 minutes on rather than multiple hours.

    Anyway, thanks everyone, from now on, I'll be using git. No way this is happening again.


  • Qt Champions 2017

    @RekTekk249 said in Tons of error for no obvious reason:

    I get this example, but I don't get how it applies to multiple files/classes. How is one declared before the other is read? I guess that means LoginWindow is "read" before MainWindow?

    There's no multiple files, not auto-magically as you think of it at least. There's 3 passes:

    1. The preprocessor: That expands all the preprocessor directives, and and your case notably the #includes. The include is literally replacing the referenced file in place of the #include directive.
    2. The compiler: Each file resulting from the above step is passed through the compiler (and if you're missing the include(s) that declare(s) the names you get the error you got). Each such file produces one object file (also known as translation unit).
    3. The linker: Taking all the object files (a.k.a. translation units) and puts them together into one binary (executable/library).


  • @kshegunov I thank you for your insights. I have learned a lot today. I've gone back on the non working projet to try and see what could have been the issue and apparently it was as you people said, but I coudn't understand: the class was used as a type before it was declared. When I checked the includes, it seemed all good, until I learned about circular include problems or something like that. I had two classes including eachother, which caused this. As someone who comes from languages that allow you to import pretty much anything and fixes the issues for you, I did not know about this.

    Thanks for enlightening me.



  • @RekTekk249 said in Tons of error for no obvious reason:

    Two hours ago, everything was going well, until I realised I needed the same instance of my helper class in my MainWindow than in my LoginWindow.

    If you would have posted the whole file, esp. showing the parts you've changed, it would have saved a lot of troubleshooting ;-)

    It's also a debugging thing... If you made changes and suddenly nothing works anymore, revert the changes and try to figure out why it happened.