none
Using CMap to Store User Defined Objects

    Question

  • Hello everyone,

    I am having trouble using CMaps in VS2008 (C++). I have looked at the 'Collect' sample that ships with VS2008 but it is a bit too convoluted to follow.

    I have created a test SDI application using the MFC project wizard. At the moment I just have a simple test object defined by a class called CBox (the typical length, width, height example that many people are familiar with).

    My objective is simply to store pointers to CBox objects in a CMap and retrieve them. The KEY is a DWORD which is the id number of the box and the VALUE is a pointer to the associated CBox. My CBox class is derived from CObject because I want to be able to serialize it to allow me to save and restore.

    My CBox is defined as follows:

    #pragma

     

    once

    #include

     

    <afxtempl.h> // MFC suport for Collections

    // CBox command target

    class

     

    CBox : public CObject
    {
    public:

    CBox();

    // Default constructor prototype

    CBox(DWORD ID, CString NAME);

    // Overload 1 constructor prototype

     

    virtual ~CBox(); // Default destructor prototype

     

    private

     

    :

    DWORD m_BoxID;

    CString m_BoxName;

    };

    typedef

     

    CMap<DWORD,DWORD,CBox*,CBox*> m_mapDWORDtoCBox;

     You can see that this is where I have defined my CMap.

    I have coded up my SDI app so that a 'Create Box' dialog is triggered by a menu command. Then I can enter values for the ID of the box (m_BoxID) and its name (m_BoxName). Everything works up to this point. I then use these ID and Name values to dynamically create a CBox. The salient lines from the OnFunctionsCreateBox() function follow. Note that because m_BoxID and m_BoxName are private members of CBox I have created a GetIDandName function to obtain their values so that they can be passed to the CBox constructor.

    DWORD BoxID;

    CString BoxName;

    m_dlgCreateBox.GetIDandName(BoxID, BoxName);

    CBox* pBox = (CBox*)

    new CBox(BoxID,BoxName);


    So far, so good. Up to this point the program compiles just fine, I know the variables are being populated with the right values as I have echoed them to a new dialog box.

    The next line causes the linker fail:

    m_mapDWORDtoCBox(DWORD,BoxID,CBox*,pBox);

    The error message is: error C2275: 'DWORD' : illegal use of this type as an expression

     

    so I tried this:

    m_mapDWORDtoCBox(BoxID,CBox*);

    The error message this time is: error C2059: syntax error : ')'

    Can anyone help?

    I've already trawled all the websites and tutorials I can find on CMaps but with no luck.

    Sunday, December 06, 2009 8:02 PM

Answers

  • A value can be added to map using

     

        m_mapDWORDtoCBox.SetAt(BoxID, pBox);

     

    or

     

        m_mapDWORDtoCBox[BoxID] = pBox;

    • Marked as answer by Wesley Yao Friday, December 11, 2009 4:07 AM
    Sunday, December 06, 2009 8:35 PM
  • What is m_mapDWORDtoCBox? You say it is a typedef, let you give it a name like a member variable. Maybe you mean something like

    typedef
    CMap<DWORD,DWORD,CBox*,CBox*> MapDWORDtoCBox;

    and then
    m_mapDWORDtoCBox could be a member of type MapDWORDtoCBox:

    MapDWORDtoCBox m_mapDWORDtoCBox;

    As to the statements that  give compiler (not linker) errors, they make no sense (at least to me). I think you mean to say

    m_mapDWORDtoCBox[BoxID] = pBox;


    David Wilkinson | Visual C++ MVP
    • Edited by davewilkMVP Sunday, December 06, 2009 8:39 PM (formatting)
    • Marked as answer by Wesley Yao Friday, December 11, 2009 4:07 AM
    Sunday, December 06, 2009 8:39 PM

All replies

  • A value can be added to map using

     

        m_mapDWORDtoCBox.SetAt(BoxID, pBox);

     

    or

     

        m_mapDWORDtoCBox[BoxID] = pBox;

    • Marked as answer by Wesley Yao Friday, December 11, 2009 4:07 AM
    Sunday, December 06, 2009 8:35 PM
  • What is m_mapDWORDtoCBox? You say it is a typedef, let you give it a name like a member variable. Maybe you mean something like

    typedef
    CMap<DWORD,DWORD,CBox*,CBox*> MapDWORDtoCBox;

    and then
    m_mapDWORDtoCBox could be a member of type MapDWORDtoCBox:

    MapDWORDtoCBox m_mapDWORDtoCBox;

    As to the statements that  give compiler (not linker) errors, they make no sense (at least to me). I think you mean to say

    m_mapDWORDtoCBox[BoxID] = pBox;


    David Wilkinson | Visual C++ MVP
    • Edited by davewilkMVP Sunday, December 06, 2009 8:39 PM (formatting)
    • Marked as answer by Wesley Yao Friday, December 11, 2009 4:07 AM
    Sunday, December 06, 2009 8:39 PM
  • Dave and Viorel,

    Thanks for your replies. I will try your suggestion and see what happens.

    ps. Don't be offended if there is a higher number than 1 for the 'Vote as helpful thing'. It's the first time I've used this Forum so I've no idea how it works.

    Thanks again for the help!
    Tuesday, December 08, 2009 7:06 PM