Sign in to follow this  
Pemaden

stl map help

Recommended Posts

I'm trying to create a map which stores pointers of a certain class based on a given string.
class Foo
{
public:
	
	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
Quote:
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this