How To Intentionally Assign Constant Value To Array?

Hello Professionals:

Good day. I came across this problem when I attempted to assign a variable to an array. For example:

1
2
3
std::unordered_map<glm::ivec3, float> g_polCoeff; 
const int count_g_polCoeff = g_polCoeff.size();
float phi[count_g_polCoeff*count_g_polCoeff];


Whenever I try to type the "count_g_polCoeff" as the size of my phi array, it turns out that I receive this kind of error whenever I hover to the count_g_polCoeff inside the phi brackets.

expression must have a constant value 
the value of variable "count_g_polCoeff" cannot be used as a constant


Having this said, I know the real size of g_polCoeff.size() (which is 1088 as of the moment), and I could have just typed my array like this way so that I won't have any problem:

1
2
const int count_g_polCoeff = g_polCoeff.size();
float phi[1088*1088];


However, I would like to use a variable there as much as possible because I will be running different OBJ files in my program, and obviously, this parameter changes every time I change my OBJ. Can you give some insights how I can solve my concern? Thank you very much for your response in advance.
Last edited on
closed account (ywbpSL3A)
Nowhere near a professional, but why don't you just define a std::vector and reserve said amount of space like so:

1
2
3
4
5
std::unordered_map<glm::ivec3, float> g_polCoeff;
const int count_g_polCoeff = g_polCoeff.size();

std::vector<float> phi;
phi.reserve( count_g_polCoeff * count_g_polCoeff );
The size of non-dynamically allocated arrays has to be known at compile time.

You might want to use a std::vector instead.

 
std::vector<float> phi(g_polCoeff.size() * g_polCoeff.size());
Last edited on
Thank you very much for your inputs everyone! :)
Topic archived. No new replies allowed.