CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 1999
    Posts
    34

    Urgent Help Needed: STL-map

    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.




  2. #2
    Join Date
    Apr 1999
    Location
    Germany
    Posts
    418

    Re: Urgent Help Needed: STL-map

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured