I have one map within one header file class:
class One
{
// code
typedef map<string, int> MapStrToInt;
inline MapStrToInt& GetDetails(unsigned long index)
{
return pData[index];
}
// populate pData....
private:
MapStrToInt *pData;
};
And a second class which implements another map and wants to get the first 10 details from the class One’s map.
class Two
{
// code
One::MapStrToInt pDataTen;
int function1()
{
for (int i =0; i < 10; i ++)
{
One::MapStrToInt * pMap = &(One::GetDetails(i));
pDataTen.insert(pair<string, int>(pMap->first,pMap->second));
}
}
}
When I compile this, it states that pMap:
has no member named ‘first’
has no member named ‘second’
Any suggestions?
Thanks..
You are using pointers to your maps, instead of plain map objects. Thus, indexing them is the same as indexing into an array of maps. (This may actually be what you want, judging from your comments.)
However,
firstandsecondare members of an element within your map, not of the map itself. So you should iterate over the map to get its individual elements, then insert them into the second map.Now, it is not quite clear whether you want to get 10 elements from the first map in your array, or 1 element each from 10 maps in your array. Here is how to do the former: