PDA

View Full Version : QHash with key and struct problem



MarkoSan
6th October 2008, 01:29
Hi to all!

In my code I have:
typedef struct merchandiseLangStruct
{
qint16 iLanguageId; // lang id
QString strMerchandiseName; // language name
};

...
...
...

class A
{
...
...
QHash<qint16, merchandiseLangStruct> m_strNames; // multilanguages names with lang. ids as keys
...
...

public:
inline QString name(const qint16& iId) const
{ return m_strNames.value(iId).m_strMerchandiseName; };
};If I try to compile this .h file, I get:
CMerchandizeElement.h: In member function `QString CMerchandizeElement::name(const qint16&) const':
CMerchandizeElement.h:91: error: 'const struct merchandiseLangStruct' has no member named 'm_strMerchandiseName'
mingw32-make[1]: *** [debug/CMerchandizeElement.o] Error 1
mingw32-make[1]: Leaving directory `C:/Documents and Settings/markofr/Desktop/erosNewCheckout/trunk/Client'
mingw32-make: *** [debug] Error 2What I am trying to do is to get a string from qhash stored inside struct. How do I do that, please help, I am sure it is some form of stupid mistake but I am getting desperate ...

yuriry
6th October 2008, 04:08
The variable name is strMerchandiseName and you are trying to get m_strMerchandiseName

MarkoSan
6th October 2008, 10:07
Oh, Jesus, stupid me, god damn, sorry for this garbage thread ...

MarkoSan
6th October 2008, 23:05
Well, is it possible to get value of key if there is a value known from QHash?

Ok, that was another stupid question, it is all in docs ... there is a key() method ... :D

But, following method:
inline qint16 merchandiseId(const QString& strKey) const
{ return m_strNames.key(strKey).iLanguageId; }; produces following errors:
In file included from CMerchandizeElement.cpp:8:
CMerchandizeElement.h: In member function `qint16 CMerchandizeElement::merchandiseId(const QString&) const':
CMerchandizeElement.h:95: error: no matching function for call to `QHash<qint16, merchandiseLangStruct>::key(const QString&) const'
c:/Qt/4.4.3/include/QtCore/../../src/corelib/tools/qhash.h:652: note: candidates are: const Key QHash<K, V>::key(const T&) const [with Key = qint16, T = merchandiseLangStruct]
c:/Qt/4.4.3/include/QtCore/../../src/corelib/tools/qhash.h:658: note: const Key QHash<K, V>::key(const T&, const Key&) const [with Key = qint16, T = merchandiseLangStruct]
mingw32-make[1]: *** [debug/CMerchandizeElement.o] Error 1
mingw32-make: *** [debug] Error 2Can someone help me please?

yuriry
6th October 2008, 23:54
Your hash is QHash<qint16, merchandiseLangStruct>, so the key() method needs to accept const merchandiseLangStruct&, and you are passing const QString&. You need to pass the struct itself.

MarkoSan
7th October 2008, 00:10
So what is the point then if I pass to this method whole struct, which already includes id?

yuriry
7th October 2008, 00:25
If you lookup by strMerchandiseName then they key should be QString.
If you lookup by iLanguageId, then the key should be qInt16.
If you lookup using both then you need 2 indexes. The index does not have to be QHash, it could be a simple sequential scan, but you need an index for every key.

MarkoSan
7th October 2008, 08:34
If you lookup by strMerchandiseName then they key should be QString.
If you lookup by iLanguageId, then the key should be qInt16.
If you lookup using both then you need 2 indexes. The index does not have to be QHash, it could be a simple sequential scan, but you need an index for every key.

Can you provide me please with some example?

yuriry
7th October 2008, 18:29
You already have QHash<qint16, merchandiseLangStruct>, use it to lookup by iLanguageId. Add another hash QHash<QString, merchandiseLangStruct> and use it to lookup by strMerchandiseName. Just be careful to update both hashes when you add/remove entries.

If the number of entries not more than approximately 500, simple sequential scan might be even faster than a hash lookup. Instead of using two QHashes, just use a array or a list to store merchandiseLangStructs and add two methods that use foreach to find the right entry in the list. Then you don't have to synchronize two QHashes when you add/remove entries.

PS: if you decide to go with two QHashes, consider storing merchandiseLangStruct* (pointers) instead of merchandiseLangStruct (values)