Thread: Map insert not working?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    24

    Map insert not working?

    map.insert fails every time. If I create a temporary map in the function it works fine.
    Code:
    class OpenGLShaderReference : public ShaderReference
    {
    	public:
    	map<std::string,OpenGLUniform*> uniforms;
    }
    
    Uniform* OpenGLShaderReference::GetUniform(std::string name)
    {
    	 OpenGLUniform* uniform = new OpenGLUniform;
             uniform->name=name;
             uniform->index = glGetUniformLocation(program,name.c_str());
                    
              this->uniforms.insert(pair<std::string,OpenGLUniform*>(name,uniform));
              if (this->uniforms[name]==NULL) Print("Failed to insert uniform");
              return uniform;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    242
    so it compiles but always prints "Failed to insert uniform"?

    if so, maybe set a breakpoint at the this->uniforms.insert(pair...); line and see if the debugger tells you anything useful.

    if it doesn't compile, i'd consider std:air and make sure you have #include <utility>
    Last edited by Aisthesis; 09-27-2010 at 07:08 PM.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    24

    Unhappy

    The debugger lists the pair "materialcolor" (the name string) and 0x00000, even though the local uniform variable is not NULL. It looks as though I did this:

    uniforms.insert(std:: pair<std::string, Uniform*>(name,NULL))

    But I did not!

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    24
    It was caused by my misunderstanding of the std::map protocols. I did not realize it was written in the worst most inconvenient way possible.

  5. #5
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Others could benefit from the information that solved your case, could you please share it?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by JMK
    It was caused by my misunderstanding of the std::map protocols. I did not realize it was written in the worst most inconvenient way possible.
    I agree with Xupicor: instead of just criticising the std::map interface, you should share what you did if you think that it was terribly inconvenient. Maybe there is a more convenient method that you simply missed. For example:
    Code:
    Uniform* OpenGLShaderReference::GetUniform(const std::string& name)
    {
        // Substitute std::unique_ptr for auto_ptr when it is available.
        std::auto_ptr<OpenGLUniform> uniform(new OpenGLUniform);
        uniform->name = name;
        uniform->index = glGetUniformLocation(program, name.c_str());
    
        if (!this->uniforms.insert(std::make_pair(name, uniform.get())).second)
        {
            Print("Failed to insert uniform");
        }
        return uniform.release();
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The only way insert will fail is if the key already exists in the map.

    I did not realize it was written in the worst most inconvenient way possible.
    I find most of the STL interfaces to be quite straightforward and easy to use. After having used .NET containers I can say for sure that the .NET generics were designed to mimic the STL interfaces except for the iterator paradigm...however, that, too shows up in much of the .NET containers and objects.

    The STL is fast enough but certainly not fast in every situation and no algorithm or design can make this claim. The STL is fast at doing what it was designed to do and yet also maintain the robustness, abstraction, and extendability that is so crucial to any template library. The STL is extremely stable, well-tested, well thought out, well designed, well maintained, and a huge time and money saver for nearly every single C++ programmer and C++ studio in the world. If you are going to make the claim it has an unwieldy interface then we would like to see some examples. Often this stems from a misunderstanding somewhere. The STL is not perfect but it is a very useful tool. I can't imagine programming today without it - I certainly would not be as productive.
    Last edited by VirtualAce; 09-28-2010 at 12:23 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would also advice you to look up smart pointers since you seem to be using raw pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with map
    By xds4lx in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2009, 01:52 AM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. how can I re-sort a map
    By indigo0086 in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2006, 06:21 AM
  4. New editor updates
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-05-2005, 03:26 PM
  5. the effects of textures on my frame rate
    By DavidP in forum Game Programming
    Replies: 37
    Last Post: 10-03-2003, 11:24 AM