[Solved]Cannot define structure at the same time
-
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...
-
@
#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...
-
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 -lQtCored4debug/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'
@ -
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(); delete Settings; return ret;
}
@This way you declare in global.h Settings to be somewhere, and define it only once in main.cpp.