danijar
danijar

Reputation: 34175

Why can't I create a std::map with this struct as value

I have the following struct.

struct ShaderObject
{
    const GLchar* File;
    ShaderType Type;
    GLuint Shader;
    ShaderObject(const GLchar* File, ShaderType Type);
};

And this type of map.

typedef map<string, ShaderObject> Shaders;

The compiler gives me an error, linking to the implementation of map.

 Error 1 error C2512: 'ShaderObject::ShaderObject': No appropriate default constructor
 available c:\program files (x86)\microsoft visual studio 11.0\vc\include\map 198 1

I don't understand the error. How can I fix it? Because I never had an error like this before and I can't explain, I am not sure which informations you will need. Please feel free to ask for details! Thanks a lot!

Upvotes: 0

Views: 809

Answers (1)

anio
anio

Reputation: 9161

You need to provide a default constructor (a constructor that can be called with no arguments) for ShaderObject if you want to put it inside std::map. The reason for this is, if you use operator[] on map for a key that doesn't exist, it will automatically insert that key and a new ShaderObject as the value. It will use the default constructor to create this object.

Upvotes: 7

Related Questions