PDA

View Full Version : Problems using QMap



importantman
27th October 2006, 21:22
I am trying to create a QMap as a seperate .h .cpp pair because it is a really long list, but doing so generates errors.

Here is the .h

#ifndef XMLSQLMAP_H
#define XMLSQLMAP_H

#include <qmap.h>
#include <qstring.h>


QMap<QString,QString> xmlsqlmap;

#endif


and here is a beginning of the .cc

#include "xmlsqlmap.h"

xmlsqlmap["PlayerID"] = "ID";
xmlsqlmap["PlayerName"] = "Name";

and here a sample of the lovely errors that result.


xmlsqlmap.cpp:3: error: ISO C++ forbids declaration of `xmlsqlmap' with no type
xmlsqlmap.cpp:3: error: size of array `xmlsqlmap' has non-integer type
xmlsqlmap.cpp:3: error: conflicting types for `int xmlsqlmap[1]'
xmlsqlmap.h:13: error: previous declaration as `QMap<QString, QString>
xmlsqlmap'
xmlsqlmap.cpp:4: error: ISO C++ forbids declaration of `xmlsqlmap' with no type
xmlsqlmap.cpp:4: error: size of array `xmlsqlmap' has non-integer type
xmlsqlmap.cpp:4: error: redefinition of `int xmlsqlmap[1]'


can anyone tell my why this happens, and how to fix it ?

danadam
27th October 2006, 23:43
#include "xmlsqlmap.h"
xmlsqlmap["PlayerID"] = "ID";
xmlsqlmap["PlayerName"] = "Name";
This is expression, and you can use expressions only in functions or methods. Outside functions and methods there are only declarations and definitions.




I am trying to create a QMap as a seperate .h .cpp pair
You should remember to have definition of the QMap in only one *.cpp file. *.h file should contain declaration.

This should work:

// .h file
#ifndef XMLSQLMAP_H
#define XMLSQLMAP_H

#include <qmap.h>
#include <qstring.h>

extern QMap<QString,QString> xmlsqlmap; // only declaration
void fill();

#endif

// .cpp file
#include "xmlsqlmap.h"

QMap<QString,QString> xmlsqlmap; // definition

void fill() {
xmlsqlmap["PlayerID"] = "ID";
xmlsqlmap["PlayerName"] = "Name";
}

// another .cpp file
#include "xmlsqlmap.h"

int main() {
fill();
}




and here a sample of the lovely errors that result.

xmlsqlmap.cpp:3: error: ISO C++ forbids declaration of `xmlsqlmap' with no type
[...]
Compiler thinks that you are trying to declare/define another variable with name xmlsqlmap.