Cannot create static QMap
-
Hello,
I have encounterd a problem. Maybe not enough headers.
I want a static QMap as class variable, but cant make it working, got two errors in QMapTest::makeStaticTest on build:invalid use of qualified-name 'QMapTest::staticMap'
or
undefined reference to `QMapTest::staticMap'qmaptest.h
@
#ifndef QMAPTEST_H
#define QMAPTEST_H#include <iostream>
#include <QObject>
#include <QMap>class QMapTest : public QObject
{
Q_OBJECTpublic:
explicit QMapTest(QObject *parent = 0);void makeNormalTest(); void makeStaticTest();
private:
QMap<int, QString*> normalMap;static QMap<int, QString*> staticMap;
};
#endif // QMAPTEST_H
@qmaptest.cpp
@
#include "qmaptest.h"QMapTest::QMapTest(QObject *parent) :
QObject(parent)
{}
// Working example
void QMapTest::makeNormalTest()
{
QMap<int, QString*> map;normalMap = map; normalMap.insert(1, new QString("one")); normalMap.insert(2, new QString("two")); QMapIterator<int, QString*> i(normalMap); while (i.hasNext()) { i.next(); QString *val = i.value(); std::cout << i.key() << " : " << qPrintable(*val) << std::endl; normalMap.remove(i.key()); delete val; }
}
// Not working
void QMapTest::makeStaticTest()
{
// Test one
// invalid use of qualified-name 'QMapTest::staticMap'
QMap<int, QString*> QMapTest::staticMap;// Test two QMap<int, QString*> map; // undefined reference to `QMapTest::staticMap' QMapTest::staticMap = map;
}
@main.cpp
@
#include "mainwindow.h"
#include "qmaptest.h"#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);QMapTest qmt; qmt.makeNormalTest(); qmt.makeStaticTest(); MainWindow w; w.show(); return a.exec();
}
@ -
welcome to devnet
[quote author="omgnull" date="1368726023"]Hello,
undefined reference to `QMapTest::staticMap'
[/quote]You need to allocate the memory for your static instance.
[quote author="omgnull" date="1368726023"]
invalid use of qualified-name 'QMapTest::staticMap'
[/quote]That error message comes when you instantiate in the wrong place.
@
#include "qmaptest.h"// you allocate instantiate the memory
QMap<int, QString*> QMapTest::staticMap; // <<< here it goesQMapTest::QMapTest(QObject *parent) :
QObject(parent)
{}
// Working example
void QMapTest::makeNormalTest()
{
QMap<int, QString*> map;normalMap = map; normalMap.insert(1, new QString("one")); normalMap.insert(2, new QString("two")); QMapIterator<int, QString*> i(normalMap); while (i.hasNext()) { i.next(); QString *val = i.value(); std::cout << i.key() << " : " << qPrintable(*val) << std::endl; normalMap.remove(i.key()); delete val; }
}
void QMapTest::makeStaticTest()
{
// Test one
// invalid use of qualified-name 'QMapTest::staticMap'
// QMap<int, QString*> QMapTest::staticMap;// Test two QMap<int, QString*> map; // undefined reference to `QMapTest::staticMap' QMapTest::staticMap = map;
}
@Change it to the qmaptest.cpp as given above and you can compile.
You need to familiarize a bit with static variables of a class. "Here is a similar case":http://www.cplusplus.com/forum/general/49548/ with a bit easier understandable initialization process.
[edit, changed the wrong formatting, koahnig]
-
I could even imagine that I can initialize static private members outside of the class. Thank you very mutch. Now it time to go deep in cpp books.