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_OBJECT
public:
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();
}
↧