I declare and initialize this constant in globals.cpp:
const unsigned SETT_OPTION_COUNT=4;
To make it accessible from other files, I make the const extern in globals.h:
extern const unsigned SETT_OPTION_COUNT;
However, when I want to use it as array size here:
1 2 3 4 5 6 7 8
|
bool create_settings(std::ofstream& file, const int values[SETT_OPTION_COUNT]) {
for (size_t i=0; i<SETT_OPTION_COUNT; i++) {
file << SETT_STRING_LOCALE[i] << "=" << values[i];
if (i<SETT_OPTION_COUNT-1)
file << std::endl;
}
return true;
}
|
I get the error that the expression must be constant.
I do realise that static arrays must have constant array size (const or as a const numerical within []), but I don't understand why SETT_OPTION_COUNT is illicit in this context.
Does it have anything to do with
extern not being used properly? I didn't get any errors pertaining to the use of
extern though.
For now, the only solution I found is to make the function count the array size itself:
bool create_settings(std::ofstream& file, const int values[]);
P.S When calling the function I use the const too:
create_settings(ofs, values[SETT_OPTION_COUNT]);
Any help appreciated :)