PDA

View Full Version : QMap issue



sajis997
23rd July 2011, 15:07
Hello forum,

I am trying to implement a graph data-structure using QMap<QString,int> and QVector<Node*> .

QMap<> contains the node name and index of the QVector so that i can accesss the Node* directly.

And i have the following function to retrieve the index for the vector to retrieve the node


//////////////////////////////////////////////////////////////////////////




int SceneHierarchy::getNodeIndex(const QString &nodeName) const
{
int pos = -1;
//find the node in the map with the key - node name
QMap<QString,int>::const_iterator iter = m_nodeMap.constFind(nodeName);

if(iter == m_nodeMap.constEnd())
{
//node name is not found in the map
pos = -1;
}
else
{
//!node found inside the map and so we
//!return the index of the node in the vector list
pos = iter.value();
}
return pos;
}



////////////////////////////////////////////////////////////////////////////


The program crashes at the following line



QMap<QString,int>::const_iterator iter = m_nodeMap.constFind(nodeName);



I do not get what could be the possible problem with the above code. If the name is not found it will simply returns constEnd(), right?

I must be missing something. Anyone in the forum help to point it out?


Thanks
Sajjad

Lykurg
23rd July 2011, 22:59
I can't see an error right now. Is m_nodeMap set up correctly? Nevertheless I would use QMap::value()

return m_nodeMap.value(nodeName, -1);