Skip to main content
GameDev.net gamedev.net
🔒 Locked

STL Map Problem

Started by MetaKnight Oct 28, 2004 at 5:48 PM 7 replies 550+ views
Original Post
MetaKnight
MetaKnight
Okay im using a map with String key and a pointer to a Texture object, but when i try to acess the Texture object with the string key, it says it doesnt exist, ill just give you relevent parts of the code: this is how i create the map:

std::map<string, lTexture*> TextureList;


this is how i add to the map:

TextureList.insert(pair<string, lTexture*>("mega.dds",new lTexture()));


and this is how i request it(this is where is says it's not there):

TextureList["mega.dds"]->Set();		


Fruny
Fruny
#include <map>#include <string>using namespace std;struct Foo{  int Set() {};};int main(){   map<string, Foo*> foo;   foo.insert(pair<string, Foo*>("mega.dds", new Foo()));   foo["mega.dds"]->Set();}


Works all right for me (g++ 3.3.4)

What's the exact error message you get?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Qw3r7yU10p!
Qw3r7yU10p!
When you say it says it doesn't exist, is this at runtime or compile time?

Is it crashing or is it not compiling?
Thygrrr
Thygrrr
Microsoft STL has some bugs AFAIK. This could be one of them? (though I think it was hashed set that was badly crippled)
Qw3r7yU10p!
Qw3r7yU10p!
Quote:
Microsoft STL has some bugs AFAIK. This could be one of them?

This wouldn't be a bug that had gone unnoticed. Meta is almost certainly doing something wrong in between or isn't posting the real code or has a macro which is messing things up.

We need to know what he means when he says "it's not there". Does he mean it won't compile (which could mean 'Set' isn't a public member of lTexture), or does he mean it crashes?
MetaKnight
MetaKnight
Sorry I wasnt more clear. I meant to say that when I use

TextureList["mega.dds"]

to get a pointer to my texture, it returns a null adress. When the adress should be to my lTexture object.

I found the problem seems to be in here:

When TextureList has a size of 0 and i check to see if there is a lTexture in it, it all of a sudden makes a entry into the map, without an insert command being called. Like this:
Requested *lTexture = NULL;// I check to see if there is a lTexture with the same file in the map// size is 0 here Requested = TextureList[fileName];// now size is 1!! out of nowhere a map entry is made


[Edited by - MetaKnight on October 29, 2004 7:37:24 AM]
Qw3r7yU10p!
Qw3r7yU10p!
yes, if you read the documentation it says:

Quote:
data_type&
operator[](const key_type& k)
Returns a reference to the object that is associated with a particular key. If the map does not already contain such an object, operator[] inserts the default object data_type().


You need to use find() to check if an entry exists. It returns an iterator pointing to the pair in the map. If it doesn't find anything it will return end().
bkt
bkt
Also suggest that you use STLport, the Microsoft STL is a bit bugged in certian areas.
-John "bKT" Bellone [homepage] [[email=j.bellone@flipsidesoftware.com]email[/email]]
MetaKnight
MetaKnight
Ah nice

I used a find insted of the [] operator, it works now, thank you all very much.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.