2

Consider the following code, I've marked the important line with #this symbol:

#include <glad/include/glad/glad.h>
#include <string>
#include <iostream>

#ifndef LAMP_H
#define LAMP_H
namespace lmp{

    class genLamp{
        unsigned int lmpVAO;
        static const float flag{1}; //#this is allowed 
        static const float default_shape[]{  //#this is not allowed
        -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
         0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,

        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
         0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
        -0.5f,  0.5f,  0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,

        -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
         0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
         0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
         0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
         0.5f, -0.5f, -0.5f,  1.0f, 1.0f,
         0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
         0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,

        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f,  0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f
    };

        genLamp(std::string vShaderPath, std::string fShaderPath){
            glGenVertexArrays(1, &lmpVAO);
            glBindVertexArray(lmpVAO);


        }

        unsigned int getVAO(){
            return this->lmpVAO;
        }

    };

}

#endif  

First of all, why is this even not allowed, what problem is the language trying to prevent by preventing this? And,

Since the default_shape array is going to be the same across objects no matter what, I was trying to share this array by making it static. But, this doesn't seem to be possible. The only thing I can think of is declaring the variable into a global scope, which is not so good in my case. Does c++ have any syntax to declare and initialize static const arrays ? I'm compiling with c++17 in case the information is useful.

EDIT: if possible please also explain @user's answer

2
  • I would suggest std::array
    – kesarling
    Commented Jun 7, 2020 at 8:11
  • Change const to constexpr.
    – QuentinUK
    Commented Jun 7, 2020 at 8:39

1 Answer 1

2

Make them inline. The following code compiles.

class Temp {
    inline static const float values[] = { 0.0f, 1.0f };
};

Or even better,

class Temp {
    constexpr static float values[] = { 0.0f, 1.0f };
};

Thanks to John for pointing this out.

8
  • 3
    GCC and Clang are also happy if it's constexpr instead. Commented Jun 7, 2020 at 8:16
  • @user , wow, that actually works, but do you mind please telling me why it didn't work and why it works now
    – juztcode
    Commented Jun 7, 2020 at 8:19
  • @JohnZwinck , what do you mean? Could you please explain a bit more?
    – juztcode
    Commented Jun 7, 2020 at 8:19
  • 1
    @juztcode, also note that static constexpr variables are implicitly inline.
    – Evg
    Commented Jun 7, 2020 at 8:32
  • 1
    @juztcode it's in the language specification. Have a read of const static members.
    – user13692250
    Commented Jun 7, 2020 at 8:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.