PDA

View Full Version : QMap with pointer to custom type



Eos Pengwern
19th November 2014, 21:17
I have a QMap of the form


QMap< myCustomType1, myCustomType2* > myMap;

where myCustomType1 and myCustomType2 are both well-tested classes with all their constructors, assignment operators and destructors defined. I want to store pointers to objects of myCustomType2, rather than the objects themselves, since the objects actually reside in an array - I want to have the choice of searching for an object either by its array index, or by its associated key, without taking separate copies of the objects.

However, when I try to populate the map:


myMap.insert(myCustomType1Object, &myCustomType2Array[i]);

...I always get an "Access violation reading location 0x00000000" thrown out form the QMap's 'detach' method (which is the first command in the source code for its 'insert' method).

I've gone through the Qt documentation carefully and I can't see any restriction on the object types that can be put into a QMap (save that the key type must define operator<(), which mine does), so why doesn't this work?

anda_skoa
20th November 2014, 06:32
Is "this", the object containing the map, a valid instance?

Cheers,
_

Eos Pengwern
20th November 2014, 07:35
Unbelievable! The solution really was that simple. I'd created my QMap as a pointer for no other reason than to avoid having to expose "#include <QMap>" in my header file, but then forgotten to instantiate it with 'new'.

That fixed the problem. Thank you very much.