Jump to content
  • Advertisement
Sign in to follow this  

stl map help

This topic is 4094 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to create a map which stores pointers of a certain class based on a given string.
class Foo
	std::map<std::string, DoS*> IdMap;
	void Enter(std::string ID, DoS* ds2)
		IdMap[ID] = ds2;   //  Error here
However when I try to run the code I get an error on the line inside the Enter method. It comes up with some unhandled exception and I'm not sure why. Stepping through it in debug mode shows that there is a string in ID and that ds2 has a valid pointer. I'm sure it's something small I've overlooked. Any ideas?

Share this post

Link to post
Share on other sites
Firstly, include the classes at the top, if you haven't already done so,

#include <string>
#include <map>

Then use this line after the includes,

using namespace std;

This eliminates the need for std::string, you can just use string instead.
This may not fix the problem, but it will help to clean up your code.

Share this post

Link to post
Share on other sites
Original post by Pemaden
You guys really amaze me at times :)

That was exactly the problem. Thanks Sneftel!

You'll probably want to add this to your standard list of things to check when debugging.

Also, try to avoid manual memory allocation. The general rule of thumb in C++ is "use X where you can and raw pointers where you have to", and there are quite a few possible stand-ins for X (references; smart pointers of various flavours; members aggregated by value rather than by pointer).

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!