PDA

View Full Version : pointer to nested QMap and QList



franki
4th December 2013, 11:42
Hi all,

I need to pass reference to: QMap<int,QList<int> > myMap
to function that should fill it in, so I did:



QMap<int,QList<int> > myMap;
this->myFunction(..other_vars...,&myMap);


inside function


void myFunction(..other_vars..,QMap<int,QList<int> > *myMap) {
if(!myMap->contains(actor_type))
myMap->insert(actor_type,QList<int>());
if(!myMap[actor_type].contains(actor_id))
myMap[actor_type].append(actor_id);
}


there is problem in last line:
error: 'class QMap<int, QList<int> >' has no member named 'append'

so I think second to last line is also problematic, but QMap and QList both have function "contains".
In short words, how to access QList elements within QMap when I passed reference to QMap ?
I could use myMap->value(actor_type) - but this returns const

best regards
Marek

Added after 11 minutes:

sorry for this lame question,
last two lines should be:
[CODE]
if(!(*myMap)[actor_type].contains(actor_id))
(*myMap)[actor_type].append(actor_id);
[CODE]
then it works.

cheers
Marek

stampede
4th December 2013, 11:44
You can:
1) pass it by reference:


QMap<int,QList<int> > myMap;
this->myFunction(..other_vars...,myMap);

void myFunction(..other_vars..,QMap<int,QList<int> > & myMap) {
if(!myMap.contains(actor_type))
myMap.insert(actor_type,QList<int>());
if(!myMap[actor_type].contains(actor_id))
myMap[actor_type].append(actor_id);
}

2) de-reference the map before using operator []:


void myFunction(..other_vars..,QMap<int,QList<int> > *myMap) {
if(!myMap->contains(actor_type))
myMap->insert(actor_type,QList<int>());
auto & map = *myMap;
if(!map[actor_type].contains(actor_id))
map[actor_type].append(actor_id);
}

3) explicit call "operator []" method


void myFunction(..other_vars..,QMap<int,QList<int> > * myMap) {
if(!myMap->contains(actor_type))
myMap->insert(actor_type,QList<int>());
if(!myMap->operator[](actor_type).contains(actor_id))
myMap->operator[](actor_type).append(actor_id);
}


myMap[i] in your code is interpreted as indexed access to myMap array (remember the pointers <-> arrays relation), not as calling operator[] method on *myMap object.