Thread: pointer to a stl map

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

    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,149
    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
    28,413
    Why do you need to dynamically allocate the map?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    269
    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
    269
    That worked! thanks!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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
    269
    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
    269
    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,336
    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,318
    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, 04:41 PM
  2. Replies: 9
    Last Post: 06-13-2009, 02:31 AM
  3. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  4. Replies: 4
    Last Post: 08-27-2007, 11:51 PM
  5. Replies: 4
    Last Post: 11-05-2006, 02:57 PM