Jump to content
  • Advertisement
Sign in to follow this  
ajm113

std::find, std::end, Exception Error, C++

This topic is 3592 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

Hello, I get a exception error on this line:
if(m_mpTextures.find( szFile ) != m_mpTextures.end()){




m_mpTextures And szFile are defined like so:
//In a class
std::map<const char*, NodeTexture*, TextureCompare> m_mpTextures;

//szFile Proccess
char *szFile;
szFile = new char[strlen(szFilename)+1];
	strcpy(szFile, szFilename);

	_strlwr( szFile );

//Then if(m_mpTextures.find( ... and so forth.


Can someone suggest what to do? I am not very good with exceptions that well.. the szFile goes through a process that gives it a value so it isn't NULL. VS2005/C++/Win32

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by ajm113
Hello, I get a exception error on this line:


I'm afraid you'll have to tell us exactly what error or exception you get.

Share this post


Link to post
Share on other sites
That's not going to work like you think it should. char *'s are not strings, so the map will not do a strcmp when it tries to find them (unless you're providing your own comparison function), it will check if the pointer addresses are equal. Use std::strings if you want to use strings as map keys.

I suspect your exception is an access violation because you aren't properly allocating the char*'s you're using as keys. Can we see where in code you do that?

Share this post


Link to post
Share on other sites
Well I get this exception error:

Unhandled exception at 0x6b94704d (msvcp80d.dll) in main.exe: 0xC0000005: Access violation reading location 0x03010101.

@Driv3MeFar, sorry I am a bit light headed right now, but I don't believe I did that in my code for char *s. Can you give me a small example what you would do?

Share this post


Link to post
Share on other sites
We'd need to see the code for TextureCompare in order to understand what is causing the crash.
However you could probably make the problem go away by just using "std::string" instead of "const char *". That'd make it a whole lot easier too.

Share this post


Link to post
Share on other sites
Quote:
Original post by ajm113
Well I get this exception error:

Unhandled exception at 0x6b94704d (msvcp80d.dll) in main.exe: 0xC0000005: Access violation reading location 0x03010101.

@Driv3MeFar, sorry I am a bit light headed right now, but I don't believe I did that in my code for char *s. Can you give me a small example what you would do?


He would just use std::string, and so would I.

Share this post


Link to post
Share on other sites

struct TextureCompare {
bool operator() (const char* s1, const char* s2) const {
return strcmp(s1, s2) < 0;
}

};




Sorry for the late reply, no one was replying a while ago, so I thought this thread was already dead.

But I tried std::strings, but they don't work that well what I been playing with.

Share this post


Link to post
Share on other sites
Quote:
Original post by ajm113
Sorry for the late reply, no one was replying a while ago, so I thought this thread was already dead.

But I tried std::strings, but they don't work that well what I been playing with.
You'll find that most of us here disagree. std::strings are what works well, it's char*'s that don't work well. If you think otherwise then you're probably doing it wrong.

Don't forget that people replying here come from all sorts of time zones. As such some people wont get to read other's replies for perhaps at least 24 hours.

Share this post


Link to post
Share on other sites
Ok, so how do I do this? I already tried changing szFile and making a local string variable out of m_mpTextures, I keep getting errors, can you tell me a correct way on doing this?


Tried #1

std::string TexturesTemp = m_mpTextures;

if(TexturesTemp.find(szFile) != TexturesTemp.end()){





Tried #2

if(m_mpTextures.find((std::string)szFile) != m_mpTextures.end()){


Share this post


Link to post
Share on other sites
Like this perhaps:

typedef std::map<std::string,NodeTexture*> TextureMap;
TextureMap textures;

// in some function
std::string name = /* whatever */;

TextureMap::iterator it = textures.find(name);
if(it != textures.end())
{
// more code
}


Better still to use a smart pointer for the NodeTextures.

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!