C++ global class varible definition
-
As I wrote here: "[Solved]Cannot define structure at the same time":http://developer.qt.nokia.com/forums/viewthread/5725/, I have similar problem:
I have class MainWindow and I want to define it as a global variable (I am compiling with MSVC 2008) but it doesn't work, compilator writes: C2143 and C4430My globals.h:
@#include <QtCore/QString>
#include <QtCore/QObject>
#include <QtCore/QLibrary>#include <QtGui/QApplication>
#include <QtGui/QMainWindow>#include "MainWindow.h"
extern MainWindow *SydneyMainWindow;@
My main.cpp:
@#include "globals.h"MainWindow* SydneyMainWindow = 0;
int main(int argc, char *argv[])
{
QApplication SydneyStudioApplication(argc, argv);SydneyMainWindow = new MainWindow(); SydneyMainWindow->show(); return SydneyStudioApplication.exec();
}
@I really don't know, where could be problem...
-
Well, the C2143 is an error which states: error C2143: syntax error : missing 'token1' before 'token2'. Check your MainWindow.h (maybe you have forgotten the ; after }).
I do not like the idea of making MainWindow a global variable - do you really need this? Have you tried to make it Singleton?
If I were you I would not put the extern to the globals.h but I would use it only in these modules which will use global pointer to the MainWindow.Cheers,
Konrad -
<flamebait>
[quote author="bergo.torino" date="1308412367"]I do not like the idea of making MainWindow a global variable - do you really need this? Have you tried to make it Singleton?[/quote]How is that not the same as making it global?
</flamebait>Now to some serious stuff; a tip: The compiler not only tells you about error codes, it also tells you exactly what causes the error. Often you can find out what the precise error is by reading the error text. If you need help interpreting this text, you should paste the entire error (including descriptive text), so people don't have to guess what the error precisely says. It helps them to help you quicker.
-
[quote author="Franzk" date="1308434430"]<flamebait>
[quote author="bergo.torino" date="1308412367"]I do not like the idea of making MainWindow a global variable - do you really need this? Have you tried to make it Singleton?[/quote]How is that not the same as making it global?
</flamebait>[/quote]
The difference is conceptual and we can get rid of externs here. Just include mainwindow.h and call instance() to play with MainWindow. Anyway it is not a cure for the problem but a workaround.
-
Hm well, I know about that. From my point of view you just have a global variable with a nice looking wrapper. 99 times out of a 100 you can use a different approach and in most of those cases you should really rethink your design: Why do you need to have MainWindow available everywhere? What does MainWindow do that you cannot delegate to some worker object that can be passed to other objects?
-
That is why I called it workaround, not a cure :)
-
Peppy the double declaration of the modifies the scope of the variable (the compiler should output a warning)
the first decleration you make (extern MainWindow SydneyMainWindow; ) line 10 globals.h is invalidated/nullified inside the main.cpp file due to the declaration in line 3: (MainWindow SydneyMainWindow = 0;)
Is this what you intended to do?
-
Is this the right code for instance?
@
class user
{private:
int id;
static int next_id;public:
static int next_user_id()
{
next_id++;
return next_id;
}/* stuff for the class */
user()
{
id = user::next_id++; //or, id = user.next_user_id();
}
};
int user::next_id = 0;
@[EDIT: code formatting, Volker]
-
[quote author="Simon6Sweet" date="1309760515"]Is this the right code for instance? @ class user { private: int id; static int next_id; public: static int next_user_id() { next_id++; return next_id; } /* stuff for the class */ user() { id = user::next_id++; //or, id = user.next_user_id(); } }; int user::next_id = 0; @ [EDIT: code formatting, Volker][/quote]
Almost. @
id = user::next_id++;
@
is not the same as
@
id = user.next_user_id();
@
which would be the same as
@
id = ++user::next_id;
@
See "here":http://www.imb-jena.de/~gmueller/kurse/c_c++/c_operid.html for an explanation.