I try to use a global variable as elements for an array. The problem is that the compiler want to know the integer constant value while declaring an array. I thought that my global variable is allready a const int???
global header file "constants.h"
#ifndef CONSTANT_H
#define CONSTANT_H
namespace constants{
extern const int MY_ROW;
extern const int MY_COLUMN;
}
#endif // CONSTANT_H
define global "constants.cpp"
#include "constants.h"
namespace constants{
const int MY_ROW{55};
}
main function "main.cpp"
#include "constants.h"
#include <iostream>
#include <array>
int main()
{
std::cout<<constants::MY_ROW<<std::endl;
int my_int_array[constants::MY_ROW];
return 0;
}
So far everything is going well, I can declare an array in main using globals as elements.
But if I try "the same thing" in another header the compiler complains.
"test.h"
#ifndef TEST_H
#define TEST_H
#include "constants.h"
class Test
{
public:
Test();
~Test();
void display_array();
private:
int test_array[constants::MY_ROW]; //error here???
};
#endif // TEST_H
error message:
**error: array bound is not an integer constant before ']' token|**
I appreciate it if someone can bring light to darkness.