Problem using map<> - Compile Errors

This is a discussion on Problem using map<> - Compile Errors within the C++ Programming forums, part of the General Programming Boards category; Hi Everybody.. i have a question. I am trying to use map<> in my VC++ project but it gives me ...

  1. #1
    Registered User intruder's Avatar
    Join Date
    Nov 2002
    Posts
    48

    Problem using map<> - Compile Errors

    Hi Everybody..

    i have a question. I am trying to use map<> in my VC++ project but it gives me compile time errors.

    It says "missing storage specifiers", i am also including #incude <map> still it gives me the same errors.

    Also do i need to use any namespace for this ?

    Pls help.

    Thanks,
    In

  2. #2
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    map is in the std namespace, IIRC.
    There is a difference between tedious and difficult.

  3. #3
    Registered User intruder's Avatar
    Join Date
    Nov 2002
    Posts
    48
    Ohh ok.. then it should work if i do a using namespace std.

    Thanks decrypt

  4. #4
    Registered User intruder's Avatar
    Join Date
    Nov 2002
    Posts
    48
    There is still one problem..

    In my project fstream.h file is already included in the root class and all my classes are inheriting this class so now everybody have the fstream.h.

    now when i do using namespace std; it gives me compile errors saying fstream ambiguous. so now how do i use map in this case ?

    Pls help.

    thanks in advance.

  5. #5
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    I'm not sure why it's doing that, but you can use the std namespace for just maps using one of two methods. Instead of map, use std::map. Otherwise, instead of "using namespace std;", put "using std::map;"

    The first designates each map as the map in the std namespace. The second tells the compiler that every time you use map, you mean the one in the std namespace.
    There is a difference between tedious and difficult.

  6. #6
    Registered User intruder's Avatar
    Join Date
    Nov 2002
    Posts
    48
    Ohh great.. that thing worked.. !!

    i am doing std::map<> now and its working fine..

    Thanks for the help decrypt

  7. #7
    Registered User intruder's Avatar
    Join Date
    Nov 2002
    Posts
    48
    hey Decrypt.. it doesn't give compile errors anymore but how to instantiate the map ? when i am trying to make an object of the map it gives me lot of warnings..

    here is my code.


    std::map<CString, double> *m_pData;

    m_pData = new map() <-- since we have a default constructor ??

    but this generates lots of warnings .. then how do i actually instantiate the map ?

    thanks.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    >> In my project fstream.h file is already included
    You shouldn't be using fstream.h, you should be using fstream. If you switch to fstream it should work and your code will be better for it.

    >> m_pData = new map();
    Is there a reason you are dynamically allocating the map? Usually it isn't necessary. If you do have a reason for doing so, you would use the full type for the map, in this case std::map<CString double>.

  9. #9
    Registered User intruder's Avatar
    Join Date
    Nov 2002
    Posts
    48
    Hi Daved,

    Its a huge framework and i cannot change it so i have to go with the fstream.h now.

    Also how can we use the map without dynamically allocating it ?

    if i am declaring as map<CString, double> *m_pData; then i need to allocate memory to the pointer right ?? before using it ?

    if not then how do i use it ? how do i insert into the map ??

    Thanks for your help.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    Just use a regular member variable (I'm assuming its a member because of the m_):
    Code:
    std::map<CString, double> m_mapData;
    To insert into the map, just call insert or use the operator[]:
    Code:
    m_mapData.insert(std::make_pair(csKey, dValue)); // or
    m_mapData[csKey] = dValue;
    There are small differences in how those work, but for most cases either would be fine.

  11. #11
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    If you want to dynamically allocate it, do:

    Code:
    map<CString,double>* m_pMap;
    
    m_pMap = new map<CString,double>();
    If you want to allocate a map on the stack, or globally, do:

    Code:
    map<CString,double> theMap;
    To insert into the map:

    Code:
    double value = 2.0;
    (*m_pMap)[CString("Key")] = value;
    To remove from the map:

    Code:
    m_pMap->erase(CString("Key"));
    To determine if there is a particular entry in the map, do:

    Code:
    if (m_pMap->find(CString("Key")) != m_pMap->end()) {
        // entry found
    }
    To iterate through all elements in the map, do:
    Code:
    map<CString,double>::iterator itr = m_pMap->begin();
    map<CString,double>::iterator end = m_pMap->end();
    for (; itr != end; itr++) {
        cout << "Key: " << itr->first() << endl;
        cout << "Value: " << itr->second() << endl;
    }
    That should be a start for you...
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  12. #12
    Registered User intruder's Avatar
    Join Date
    Nov 2002
    Posts
    48
    ok i have changed it from a pointer to a normal member variable .. so now my code is ..

    map<CString, double> m_dataMap;

    but when i compile it gives me around 26 warnings

    some of them are here.

    c:\program files\microsoft visual studio\vc98\include\xtree(200) : warning C4786: '?rbegin@?$_Tree@VCString@@U?$pair@$$CBVCString@@N @std@@U_Kfn@?$map@VCString@@NU?$less@VCString@@@st d@@V?$allocator@N@3@@3@U?$less@VCString@@@3@V?$all ocator@N@3@@std@@
    QAE?AV?$reverse_bidirectional_iterator@Viterator@? $_Tree@VCString@@U?$pair@$$CBVCString@@N@std@@U_Kf n@?$map@VCString@@NU?$less@VCString@@@std@@V?$allo cator@N@3@@3@U?$less@VCString@@@3@V?$allocator@N@3 @@std@@U?$pair@$$CBVCString@@N@3@AAU43@PAU43@H@2@
    XZ' : identifier was truncated to '255' characters in the browser information
    c:\program files\microsoft visual studio\vc98\include\map(46) : see reference to class template instantiation 'std::_Tree<class CString,struct std:air<class CString const ,double>,struct std::map<class CString,double,struct std::less<class
    CString>,class std::allocator<double> >::_Kfn,struct std::less<class CString>,class std::allocator<double> >' being compiled
    c:\vss\appcode\pricing\branches\may06_version7.40\ mbscollection\ml2pricingmodel.h(102) : see reference to class template instantiation 'std::map<class CString,double,struct std::less<class CString>,class std::allocator<double> >' being compi
    led
    c:\program files\microsoft visual studio\vc98\include\xtree(202) : warning C4786: '?rbegin@?$_Tree@VCString@@U?$pair@$$CBVCString@@N @std@@U_Kfn@?$map@VCString@@NU?$less@VCString@@@st d@@V?$allocator@N@3@@3@U?$less@VCString@@@3@V?$all ocator@N@3@@std@@
    QBE?AV?$reverse_bidirectional_iterator@Vconst_iter ator@?$_Tree@VCString@@U?$pair@$$CBVCString@@N@std @@U_Kfn@?$map@VCString@@NU?$less@VCString@@@std@@V ?$allocator@N@3@@3@U?$less@VCString@@@3@V?$allocat or@N@3@@std@@U?$pair@$$CBVCString@@N@3@ABU43@PBU4
    3@H@2@XZ' : identifier was truncated to '255' characters in the browser information
    c:\program files\microsoft visual studio\vc98\include\map(46) : see reference to class template instantiation 'std::_Tree<class CString,struct std:air<class CString const ,double>,struct std::map<class CString,double,struct std::less<class
    CString>,class std::allocator<double> >::_Kfn,struct std::less<class CString>,class std::allocator<double> >' being compiled
    c:\vss\appcode\pricing\branches\may06_version7.40\ mbscollection\ml2pricingmodel.h(102) : see reference to class template instantiation 'std::map<class CString,double,struct std::less<class CString>,class std::allocator<double> >' being compi
    led
    c:\program files\microsoft visual studio\vc98\include\xtree(204) : warning C4786: '?rend@?$_Tree@VCString@@U?$pair@$$CBVCString@@N@s td@@U_Kfn@?$map@VCString@@NU?$less@VCString@@@std@ @V?$allocator@N@3@@3@U?$less@VCString@@@3@V?$alloc ator@N@3@@std@@QA
    E?AV?$reverse_bidirectional_iterator@Viterator@?$_ Tree@VCString@@U?$pair@$$CBVCString@@N@std@@U_Kfn@ ?$map@VCString@@NU?$less@VCString@@@std@@V?$alloca tor@N@3@@3@U?$less@VCString@@@3@V?$allocator@N@3@@ std@@U?$pair@$$CBVCString@@N@3@AAU43@PAU43@H@2@XZ
    ' : identifier was truncated to '255' characters in the browser information
    c:\program files\microsoft visual studio\vc98\include\map(46) : see reference to class template instantiation 'std::_Tree<class CString,struct std:air<class CString const ,double>,struct std::map<class CString,double,struct std::less<class
    CString>,class std::allocator<double> >::_Kfn,struct std::less<class CString>,class std::allocator<double> >' being compiled
    c:\vss\appcode\pricing\branches\may06_version7.40\ mbscollection\ml2pricingmodel.h(102) : see reference to class template instantiation 'std::map<class CString,double,struct std::less<class CString>,class std::allocator<double> >' being compi
    led

    i don't know why this happens ?

  13. #13
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    # pragma warning (disable:4786)
    # include <map>
    using namespace std;
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  14. #14
    Registered User intruder's Avatar
    Join Date
    Nov 2002
    Posts
    48
    ok but now when i am doing

    map<CString, double> m_DataMap(CString, double); its not generating any warnings anymore ..

    but when i try to insert an element into the map like ..

    CString str = "AA";
    m_DataMap[str] = 1.2;

    then it gives me compilation error saying ..

    c:\vss\appcode\pricing\branches\may06_version7.40\ mbscollection\ml2pricingmodel.cpp(483) : error C2677: binary '[' : no global operator defined which takes type 'class CString' (or there is no acceptable conversion)
    Error executing cl.exe.

    why is this now ?

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    >> map<CString, double> m_DataMap(CString, double);

    That should be:

    std::map<CString, double> m_DataMap;

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile Errors for CreateThread..
    By gopi_tony in forum Windows Programming
    Replies: 20
    Last Post: 10-22-2006, 09:58 AM
  2. DirectX9 compile errors
    By maxthecat in forum Windows Programming
    Replies: 5
    Last Post: 01-01-2006, 09:33 PM
  3. A really weird problem. I understand the error, but not a clue why I get the errors.
    By Finchie_88 in forum Networking/Device Communication
    Replies: 1
    Last Post: 02-20-2005, 08:48 AM
  4. Problem during compile (iostream.h error)
    By JoJo in forum C Programming
    Replies: 4
    Last Post: 04-29-2003, 06:58 PM
  5. getting massive compile errors, need help
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2003, 03:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21