Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    C++ global class varible definition

    C++ Gurus
    6
    10
    10600
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      Peppy last edited by

      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 C4430

      My 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(&#41;;
      

      }
      @

      I really don't know, where could be problem...

      1 Reply Last reply Reply Quote 0
      • B
        bergo.torino last edited by

        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

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

        1 Reply Last reply Reply Quote 0
        • P
          Peppy last edited by

          Maybe you are right...

          1 Reply Last reply Reply Quote 0
          • F
            Franzk last edited by

            <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.

            "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

            1 Reply Last reply Reply Quote 0
            • B
              bergo.torino last edited by

              [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.

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

              1 Reply Last reply Reply Quote 0
              • F
                Franzk last edited by

                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?

                "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

                1 Reply Last reply Reply Quote 0
                • B
                  bergo.torino last edited by

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

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

                  1 Reply Last reply Reply Quote 0
                  • K
                    korkakak last edited by

                    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?

                    Farm XP

                    1 Reply Last reply Reply Quote 0
                    • S
                      Simon6Sweet last edited by

                      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]

                      [url=http://www.marque-magnetics.com]Transformer Engineers[/url]

                      1 Reply Last reply Reply Quote 0
                      • L
                        loladiro last edited by

                        [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.

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post