Search code examples
c++visual-studio-2010glm-math

In old c++ (<c++11), initializing struct gets errors


struct Material {
    glm::vec3 ambient;
    glm::vec3 diffuse;
    glm::vec3 specular;
    float shininess;
};

Material emerald{ { 0.0215f, 0.1745f, 0.0215f },{ 0.07568f, 0.61424f, 0.07568f },{ 0.633f, 0.727811f, 0.633f },0.6f };

This works perfectly fine in C++11, but I can't figure out how to fix this in C++ < C++11. I migrated to VS2010 for a reason, and need to fix these errors.

the error i get is:

looks like a function definition, but there is no formal parameter list; skipping apparent body

Thank you in advance.


Solution

  • glm::vec3 (is a typedef for a class that) has a constructor that accepts three arguments, so (before C++11) you can't use uniform or aggregate initialisation for your struct.

    To do what you wish, change

    Material emerald{ { 0.0215f, 0.1745f, 0.0215f },{ 0.07568f, 0.61424f, 0.07568f },{ 0.633f, 0.727811f, 0.633f },0.6f };
    

    to

    Material emerald = { glm::vec3(0.0215f, 0.1745f, 0.0215f),
                      glm::vec3(0.07568f, 0.61424f, 0.07568f),
                      glm::vec3(0.633f, 0.727811f, 0.633f),
                      0.6f };