Click to See Complete Forum and Search --> : Urgent Help Needed: STL-map


Dion
July 20th, 1999, 03:11 PM
I try to create a map with MyID (ULONG) and pointers to CMyBaseClass ( or even void pointers). The code will compile and run fine, however when I check the content of the map, I got garbage. I have spent a couple of days on this one, if anybody can help, I really appreciate. Here is the code snippets:


#include <map>
typedef std::map< ULONG, CMyBaseObject*, std::less<ULONG> > MYID2MYBASEOBJECT;
typedef MYID2MYBASEOBJECT::iterator MYID2MYBASEOBJECTIT;
......
MYID2MYBASEOBJECT m_mapIDToObject;
......
CMyDrivedObject::MyFunction()
{
......
m_mapIDToObject.insert( MYID2MYBASEOBJECT ::value_type(MyID, (CMyBaseObject*) this) );

//My Test Code
MYID2MYBASEOBJECTIT MapIt = m_mapIDToObject.find( MyID );
CMyBaseObject* pObject = NULL;
pObject = (CMyBaseObject*) (*MapIt).second;
//End of Test Code
}



I am desperated, please help.

Martin Speiser
July 20th, 1999, 03:40 PM
Hi Dion,

can't find the error, but your code looks confusing to me. What do you think about this?

#include <map>
// do you need a sorted map? If no, delete the third parameter
typedef std::map< ULONG, CMyBaseObject*, std::less<ULONG> > MYID2MYBASEOBJECT;
typedef MYID2MYBASEOBJECT::iterator MYID2MYBASEOBJECTIT;
......
MYID2MYBASEOBJECT m_mapIDToObject;
......
CMyDrivedObject::MyFunction()
{
......
m_mapIDToObject[ MyID ] = (CMyBaseObject*)this;
// Test code
CMyBaseObject* pObject = m_mapIDToObject[ MyID ];
}



Maybe this works.


Martin