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. [Solved]Cannot define structure at the same time
Forum Updated to NodeBB v4.3 + New Features

[Solved]Cannot define structure at the same time

Scheduled Pinned Locked Moved C++ Gurus
6 Posts 2 Posters 4.9k 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

    Hi, I have this structure in globals.h:
    @
    struct Configuration
    {
    public:
    void setServerIp(QHostAddress ip) { ServerIp = ip; }
    void setGameBackground(QString bg) { BackgroundImage = bg; }
    void setStickColor(QColor clr) { StickColor = clr; }
    void setUsername(QString user) { Username = user; }

       QHostAddress getServerIp(void) { return ServerIp; }
       QString getGameBackground(void) { return BackgroundImage; }
       QColor getStickColor(void) { return StickColor; }
       QString getUserName(void) { return Username; }
    

    private:
    QHostAddress ServerIp;
    QString BackgroundImage;
    QColor StickColor;
    QString Username;
    };
    @

    I can't write in my globals.h file this:
    @
    struct Configuration
    { ... } *Settings;
    @

    Compilator writes something like "it already has been declared in ..." and opens my editor (Qt Creator) in qatomic_i386.h at function
    @inline bool QBasicAtomicInt::ref() { @

    But If I define Configuration* Settings in main.cpp (as a global var), it works, I would like to know where is the problem?

    I am including globals.h in every header file cause globals.h including Qt headers...

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      Can you post your complete globals.h file, please.

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

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Peppy
        wrote on last edited by
        #3

        @
        #ifndef GLOBALS_H
        #define GLOBALS_H

        #include <QtCore/QMapIterator>
        #include <QtCore/QTextCodec>
        #include <QtCore/QVariant>
        #include <QtCore/QMapData>
        #include <QtCore/QString>
        #include <QtCore/QObject>
        #include <QtCore/QFile>
        #include <QtCore/QMap>

        #include <QtGui/QApplication>
        #include <QtGui/QMessageBox>
        #include <QtGui/QMainWindow>
        #include <QtGui/QMenuBar>
        #include <QtGui/QPainter>
        #include <QtGui/QAction>
        #include <QtGui/QImage>
        #include <QtGui/QColor>
        #include <QtGui/QMenu>

        #include <QtGui/QGraphicsScene>
        #include <QtGui/QGraphicsView>
        #include <QtGui/QGraphicsWidget>
        #include <QtGui/QGraphicsItem>

        #include <QtSvg/QSvgRenderer>

        #include <QtXml/QDomAttr>
        #include <QtXml/QDomDocument>
        #include <QtXml/QDomElement>
        #include <QtXml/QDomNode>
        #include <QtXml/QDomNodeList>
        #include <QtXml/QDomNamedNodeMap>

        #include <QtNetwork/QHostAddress>

        struct Configuration
        {
        public:
        void setServerIp(QHostAddress ip) { ServerIp = ip; }
        void setGameBackground(QString bg) { BackgroundImage = bg; }
        void setStickColor(QColor clr) { StickColor = clr; }
        void setUsername(QString user) { Username = user; }

           QHostAddress getServerIp(void) { return ServerIp; }
           QString getGameBackground(void) { return BackgroundImage; }
           QColor getStickColor(void) { return StickColor; }
           QString getUserName(void) { return Username; }
        

        private:
        QHostAddress ServerIp;
        QString BackgroundImage;
        QColor StickColor;
        QString Username;

        } *Settings; // <== this line makes problems
        #endif // GLOBALS_H
        @

        That's all, but this doesn't work...

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Peppy
          wrote on last edited by
          #4

          MINGW output:
          @
          ...
          g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -mthreads -Wl -Wl,-subsystem,windows -o debug/clovece.exe debug/main.o debug/MainWindow.o debug/GraphicsView.o debug/NewGameWidget.o debug/moc_MainWindow.o debug/moc_GraphicsView.o debug/moc_NewGameWidget.o debug/qrc_resources.o -L'c:/Qt/2010.05/qt/lib' -lmingw32 -lqtmaind -lQtSvgd4 -lQtXmld4 -lQtGuid4 -lQtNetworkd4 -lQtCored4

          debug/MainWindow.o: In function `MainWindow':

          C:\Qt\projects\clovece-build-desktop/../../2010.05/qt/include/QtCore/../../src/corelib/arch/qatomic_i386.h:113: multiple definition of `Settings'

          debug/main.o:C:\Qt\projects\clovece-build-desktop/../clovece/main.cpp:6: first defined here

          debug/GraphicsView.o: In function `GraphicsView':

          C:\Qt\projects\clovece-build-desktop/../../2010.05/qt/include/QtCore/../../src/corelib/arch/qatomic_i386.h:113: multiple definition of `Settings'

          debug/main.o:C:\Qt\projects\clovece-build-desktop/../clovece/main.cpp:6: first defined here

          debug/NewGameWidget.o: In function `NewGameWidget':

          C:\Qt\projects\clovece-build-desktop/../clovece/NewGameWidget.cpp:3: multiple definition of `Settings'

          debug/main.o:C:\Qt\projects\clovece-build-desktop/../clovece/main.cpp:6: first defined here

          debug/moc_MainWindow.o:C:\Qt\projects\clovece-build-desktop/../../2010.05/qt/include/QtCore/../../src/corelib/global/qglobal.h:1381: multiple definition of `Settings'

          debug/main.o:C:\Qt\projects\clovece-build-desktop/../clovece/main.cpp:6: first defined here

          debug/moc_GraphicsView.o:C:\Qt\projects\clovece-build-desktop/../../2010.05/qt/include/QtCore/../../src/corelib/global/qglobal.h:1381: multiple definition of `Settings'

          debug/main.o:C:\Qt\projects\clovece-build-desktop/../clovece/main.cpp:6: first defined here

          debug/moc_NewGameWidget.o:C:\Qt\projects\clovece-build-desktop/../../2010.05/qt/include/QtCore/../../src/corelib/global/qglobal.h:1381: multiple definition of `Settings'

          mingw32-make[1]: Leaving directory `C:/Qt/projects/clovece-build-desktop'

          mingw32-make: Leaving directory `C:/Qt/projects/clovece-build-desktop'

          debug/main.o:C:\Qt\projects\clovece-build-desktop/../clovece/main.cpp:6: first defined here

          collect2: ld returned 1 exit status

          mingw32-make[1]: *** [debug/clovece.exe] Error 1

          mingw32-make: *** [debug] Error 2

          The process "C:/Qt/2010.05/mingw/bin/mingw32-make.exe" exited with code %2.
          Error while building project clovece (target: Desktop)
          When executing build step 'Make'
          @

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            If you declare it without extern, it's defined in every source file where you include globals.h.

            To achieve what you want do the following:

            in globals.h:
            @

            struct Configuration {
            // your members and methods here
            };

            extern Configuration *Settings;
            @

            in main.cpp:
            @
            #include "globals.h"

            Configuration *Settings = 0;

            int main(int argc, char *argv[])
            {
            QApplication a(argc, argv);

            Settings = new Configuration;
            
            qDebug() << Settings->getServerIp();
            int ret = a.exec&#40;&#41;;
            delete Settings;
            return ret;
            

            }
            @

            This way you declare in global.h Settings to be somewhere, and define it only once in main.cpp.

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

            1 Reply Last reply
            0
            • P Offline
              P Offline
              Peppy
              wrote on last edited by
              #6

              Yes, it works fine now, thanks.

              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