0

I am trying to add / delete values from a hashmap I created and stored into a void pointer. However the code I have for adding values seems to give me this error: "Unhandled exception at 0x75B5C41F in Project1.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0027F7C0."

The code for this: http://pastebin.com/FPzz05rU So what did I do wrong here and how can I fix it?

More information can be provided if needed.

4
  • the index is out of range, check the value of the index Commented Nov 15, 2013 at 14:01
  • Thanks for the comment. However I only have a single hashmap which is created here: pastebin.com/mLtxn7J7 The indexes I've tried are 0 and 1, both fail. Commented Nov 15, 2013 at 14:08
  • I am pretty sure that the cast is invalid there and you invoke UB by using its result. Commented Nov 15, 2013 at 14:25
  • I've tried a ton of things to no avail. So how might I fixed the undefined behavior? Commented Nov 15, 2013 at 14:31

1 Answer 1

1

On this line,

 map->at(key).push_back(value);

there is no vector at the key that you provide, so hash_map::at is throwing an std::out_of_range exception. Unlike hash_map::[], hash_map::at does not create a new element in the map if it does not already exist.

Sign up to request clarification or add additional context in comments.

3 Comments

So how would I got about adding the new element? I've tried tons of things and still haven't figured it out.
You can use hash_map::operator[] in place of hash_map::at in your code, if it's OK to automatically insert the value when it doesn't exist, or you can use hash_map::insert before calling StrVectorMap_AddValue.
Ah that rocks, using the operator[] worked perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.