简体   繁体   中英

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.

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 };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM