June 18, 2011

Peppy Peppy
Hobby Entomologist
251 posts

C++ global class varible definition

 

As I wrote here: Cannot define structure at the same time [developer.qt.nokia.com], 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 C4430

My globals.h:

  1. #include <QtCore/QString>
  2. #include <QtCore/QObject>
  3. #include <QtCore/QLibrary>
  4.  
  5. #include <QtGui/QApplication>
  6. #include <QtGui/QMainWindow>
  7.  
  8. #include "MainWindow.h"
  9.  
  10. extern MainWindow *SydneyMainWindow;

My main.cpp:

  1. #include "globals.h"
  2.  
  3. MainWindow* SydneyMainWindow = 0;
  4. int main(int argc, char *argv[])
  5. {
  6.     QApplication SydneyStudioApplication(argc, argv);
  7.  
  8.     SydneyMainWindow = new MainWindow();
  9.     SydneyMainWindow->show();
  10.  
  11.     return SydneyStudioApplication.exec();
  12. }

I really don’t know, where could be problem…

9 replies

June 18, 2011

bergo.torino bergo.torino
Ant Farmer
41 posts

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

 Signature 

It’s compiling - mostly I’m slacking off ;)

June 18, 2011

Peppy Peppy
Hobby Entomologist
251 posts

Maybe you are right…

June 19, 2011

Franzk Franzk
Robot Herder
830 posts

<flamebait>

bergo.torino wrote:
I do not like the idea of making MainWindow a global variable – do you really need this? Have you tried to make it Singleton?

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.

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

June 19, 2011

bergo.torino bergo.torino
Ant Farmer
41 posts
Franzk wrote:
<flamebait>
bergo.torino wrote:
I do not like the idea of making MainWindow a global variable – do you really need this? Have you tried to make it Singleton?

How is that not the same as making it global?
</flamebait>

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.

 Signature 

It’s compiling - mostly I’m slacking off ;)

June 19, 2011

Franzk Franzk
Robot Herder
830 posts

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?

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

June 19, 2011

bergo.torino bergo.torino
Ant Farmer
41 posts

That is why I called it workaround, not a cure :)

 Signature 

It’s compiling - mostly I’m slacking off ;)

June 27, 2011

korkakak korkakak
Ant Farmer
33 posts

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?

 Signature 

Farm XP

July 4, 2011

Simon6Sweet Simon6Sweet
Lab Rat
1 posts

Is this the right code for instance?

  1. class user
  2. {
  3.  
  4.   private:
  5.   int id;
  6.   static int next_id;
  7.  
  8.   public:
  9.  
  10.   static int next_user_id()
  11.   {
  12.     next_id++;
  13.     return next_id;
  14.   }
  15.  
  16.   /* stuff for the class */
  17.  
  18.   user()
  19.   {
  20.     id = user::next_id++; //or, id = user.next_user_id();
  21.   }
  22. };
  23. int user::next_id = 0;

[EDIT: code formatting, Volker]

 Signature 

Transformer Engineers

July 4, 2011

loladiro loladiro
Gene Splicer
596 posts

Simon6Sweet wrote:
Is this the right code for instance?
  1.  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]

Almost.
  1. id = user::next_id++;

is not the same as
  1. id = user.next_user_id();

which would be the same as
  1. id = ++user::next_id;

See here [imb-jena.de] for an explanation.

 
  ‹‹ Pointer usage in Qt      Variables in QML? ››

You must log in to post a reply. Not a member yet? Register here!