while leaning about QHash and serializing QHash to DataStream I got an error with the following code.

typedef QHash <quint32,QString> hashtype1;
typedef QHash <QLocale::Language,hashtype1> hashtype;

hashtype1 hash;
hash.insert(1, "Key1");
hash.insert(2, "Key2");

hashtype hash1;
hash1.insert(QLocale::English, hash);
hash1.insert(QLocale::French, hash);

QByteArray ba;
QByteArray ba1;
QDataStream ds(&ba, QIODevice::ReadWrite);
QDataStream ds1(&ba1, QIODevice::ReadWrite);
ds << hash;
ds1 << hash1;

qDebug() << ds.device()->readAll();
ds.device()->reset();
ds1.device()->reset();

hashtype1 hashcopy;
ds >> hashcopy;

hashtype hash1copy;
ds1 >> hash1copy;

The last statement is giving an error saying /usr/include/qt4/QtCore/qdatastream.h:362: error: no match for ‘operator>>’ in ‘in >> k’

I am not able to correct this.. Am I doing somthing wrong?? How can I correct this?

有帮助吗?

解决方案

The problem is that there are no QDataStream operators for QLocale::Language. When streaming out this works because it gets automatically converted to an integer type. It can't do this for streaming in. So you will either need to change your QHash to use a different template parameter for the key or write streaming operators for QLocale::Language (which should be trivial, you just need to cast it to/from int).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top