Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. C++ global class varible definition
Qt 6.11 is out! See what's new in the release blog

C++ global class varible definition

Scheduled Pinned Locked Moved C++ Gurus
10 Posts 6 Posters 12.2k Views 1 Watching
  • 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 Offline
    P Offline
    Peppy
    wrote on last edited by
    #1

    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
    0
    • B Offline
      B Offline
      bergo.torino
      wrote on last edited by
      #2

      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
      0
      • P Offline
        P Offline
        Peppy
        wrote on last edited by
        #3

        Maybe you are right...

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Franzk
          wrote on last edited by
          #4

          <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
          0
          • B Offline
            B Offline
            bergo.torino
            wrote on last edited by
            #5

            [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
            0
            • F Offline
              F Offline
              Franzk
              wrote on last edited by
              #6

              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
              0
              • B Offline
                B Offline
                bergo.torino
                wrote on last edited by
                #7

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

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

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  korkakak
                  wrote on last edited by
                  #8

                  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
                  0
                  • S Offline
                    S Offline
                    Simon6Sweet
                    wrote on last edited by
                    #9

                    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
                    0
                    • L Offline
                      L Offline
                      loladiro
                      wrote on last edited by
                      #10

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

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved