Tons of error for no obvious reason
-
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.
-
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 whatMainWindow
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.
-
Now two people told you where to look (in mainwindow.h) you still ignore us... I do the same with you. Good luck.
-
@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 seesMainWindow
, 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 whatMainWindow
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.
-
@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:
- The preprocessor: That expands all the preprocessor directives, and and your case notably the
#include
s. The include is literally replacing the referenced file in place of the#include
directive. - 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).
- The linker: Taking all the object files (a.k.a. translation units) and puts them together into one binary (executable/library).
- The preprocessor: That expands all the preprocessor directives, and and your case notably the
-
@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.