Jump to content
  • Advertisement
Sign in to follow this  
Niddles

Problem with std::map

This topic is 3828 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 am using std::map to manage textures, and I have a function that returns the texture id in GLuint form, but when I try to return the texture the compiler gives me a funky error. Here is the specific function:
GLuint TextureManager::GetImage(const std::string &name) const
{
    return TextureImages[name];
}
The error is: texture.cpp:77: error: passing `const std::map<std::string, GLuint, std::less<std::string>, std::allocator<std::pair<const std::string, GLuint> > >' as `this' argument of `_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = std::string, _Tp = GLuint, _Compare = std::less<std::string>, _Alloc = std::allocator<std::pair<const std::string, GLuint> >]' discards qualifiers Can someone tell me what is wrong?

Share this post


Link to post
Share on other sites
Advertisement
The index operator from std::map can modify the content if there isn't the string that goes to your function.

Use the find method and return the value that is in the returned iterator.

Edit: Ahh beaten by Sneftel

Share this post


Link to post
Share on other sites
TextureManager::GetImage() is a const member function, however the function operator[] is not a const member function of std::map. The reason it isn't const is because operator[] returns a reference to the element you're accessing. Additionally, what is problematic about that operator is that if an element with the specified key is not found, one is added to the map.

To fix this use member function find() on the map instead.

Share this post


Link to post
Share on other sites
Okay so I have a map that is <filenames, GLuints> and I want to pass this into glGenTextures, how do I do that? There's gotta be a way like you do with std::vector right?

In otherwords I want to take the ->second of the map make it look like an array and pass it into glGenTextures.

Share this post


Link to post
Share on other sites
There's no way to make it "look like" an array, because it isn't an array. Iterate through the entries in the map, passing the value of each one to glGenTextures in turn.

Share this post


Link to post
Share on other sites
Quote:
Original post by Niddles
In otherwords I want to take the ->second of the map make it look like an array and pass it into glGenTextures.


You cant do this.

You can do it with vector because the internal representation of std::vector is an array. However the internal representation of std::map is a red-black tree.

[edit]

Damnit, I got ninja'd by sneftel twice.

Share this post


Link to post
Share on other sites
I don't know. How many times do you plan to call it? If it's less than "a bazillion", then no, it won't be noticeably slow. Certainly it takes much less time to generate the texture name than it will to load the texture data in.

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!