pointer to a stl map

This is a discussion on pointer to a stl map within the C++ Programming forums, part of the General Programming Boards category; Hi, I have some pointer to a map, because I want to be able to dynamically allocate this map at ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    245

    pointer to a stl map

    Hi,
    I have some pointer to a map, because I want to be able to dynamically allocate this map at a certain time, however, I can't seem to assign any values or keys to the map. Here's some mockup code (which gives me the same error)

    Code:
    #include<map>
    #include<stdio.h>
    
    using namespace std;
    
    int main()
    {
      map<int, char*> * the_map;
      the_map = new map<int, char*>();
      the_map[0] = "zero";
      char * val = the_map[0];
      printf("val: %s.\n", val);
      return 0;
    }
    The error is:
    Code:
    stupid.cpp: In function ‘int main()’:
    stupid.cpp:10: error: no match for ‘operator=’ in ‘* the_map = "zero"’
    /usr/include/c++/4.2.1/bits/stl_map.h:226: note: candidates are: std::map<_Key, _Tp, _Compare, _Alloc>& std::map<_Key, _Tp, _Compare, _Alloc>::operator=(const std::map<_Key, _Tp, _Compare, _Alloc>&) [with _Key = int, _Tp = char*, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, char*> >]
    stupid.cpp:11: error: cannot convert ‘std::map<int, char*, std::less<int>, std::allocator<std::pair<const int, char*> > >’ to ‘char*’ in initialization
    What have I missed?

    Thank you so much!

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    That's the right syntax for assigning keys to a map, except what you have is a map pointer. Dereference the pointer, then index into the map.

    Also, if you're putting string literals in, the type should be const char *.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,312
    Why do you need to dynamically allocate the map?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    245
    Quote Originally Posted by laserlight View Post
    Why do you need to dynamically allocate the map?
    The map is contained inside some struct, which is allocated via malloc.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    245
    That worked! thanks!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by dayalsoap View Post
    The map is contained inside some struct, which is allocated via malloc.
    Why are you allocating things with malloc? Why not use new, which will automatically construct all the bits inside the struct for you so you wouldn't have to go through the insanity of using a pointer to a map?

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    245
    Quote Originally Posted by tabstop View Post
    Why are you allocating things with malloc? Why not use new, which will automatically construct all the bits inside the struct for you so you wouldn't have to go through the insanity of using a pointer to a map?
    Much neater, thank you!!

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    245
    Is it OK to put this struct in a header file then, with a constructor that does some conditional initialization?

    Or would the code for the constructor have to go in my .cpp file?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Generally speaking, definitions of structs are exactly what header files are built for. Just like with a class, you can inline the constructor inside the struct definition if it's nice'n'short, or you can define it separately in a .cpp file.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Basically if you have a pointer to a map then your program is wrong.
    To my knowledge. nobody who has asked a question on here has ever dynamically allocated a map, where that was the right thing to do.
    A std::map is dynamically allocated internally already, you don't ever need to dynamically allocate it.
    You also should not use malloc in a C++ program.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-30-2009, 05:41 PM
  2. Replies: 9
    Last Post: 06-13-2009, 03:31 AM
  3. Replies: 1
    Last Post: 03-24-2008, 11:16 AM
  4. Replies: 4
    Last Post: 08-28-2007, 12:51 AM
  5. Replies: 4
    Last Post: 11-05-2006, 02:57 PM

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