PDA

View Full Version : QMap Problem with arguments.



ankurjain
24th May 2006, 11:03
hi all,
i m making a QMap just like below:


QMap<QString,stack&> undoMap,redoMap;


and the error i get is as below:


C:\Qt\4.1.3\include\QtCore\../../src/corelib/tools/qmap.h(159) : warning C4181: qualifier applied to reference type ignored
..\include\mapStack.h(8) : see reference to class template instantiation 'QMap<class QString,class stack &>' being compiled


if i change the second parameter to pointer, it sompiles fine. can't we pass anything as parameters in QMap as well as QHash.

When shall i use QMap and QHash ?

Thanx in advance

jpn
24th May 2006, 12:12
You cannot store references.

From docs (http://doc.trolltech.com/4.1/containers.html#the-container-classes):


The values stored in the various containers can be of any assignable data type. To qualify, a type must provide a default constructor, a copy constructor, and an assignment operator. This covers most data types you are likely to want to store in a container, including basic types such as int and double, pointer types, and Qt data types such as QString, QDate, and QTime, but it doesn't cover QObject or any QObject subclass (QWidget, QDialog, QTimer, etc.). If you attempt to instantiate a QList<QWidget>, the compiler will complain that QWidget's copy constructor and assignment operators are disabled. If you want to store these kinds of objects in a container, store them as pointers, for example as QList<QWidget *>.



When shall i use QMap and QHash ?
QMap:

This provides a dictionary (associative array) that maps keys of type Key to values of type T. Normally each key is associated with a single value. QMap stores its data in Key order; if order doesn't matter QHash is a faster alternative.
QHash:

This has almost the same API as QMap, but provides significantly faster lookups. QHash stores its data in an arbitrary order.

Use associative containers like map or hash when you want to associate values to some specific keys. Search google for more information on "STL containers".