+ 2
Array won't accept constant variable as size
I'm a little confused at the compiler giving me an odd error. I've never seen it happen before, and it started complaining about it only recently. Here's a snippet below: static const int MAX_STACK_SIZE = 1024; static item stack[MAX_STACK_SIZE]; Item is a struct datatype, if you're curious. The error I'm getting is located when inserting the MAX_STACK_SIZE variable into the stack. The IDE gives me a warning ("array length expression must be const"). But this is odd ... the stack size is a constant. O.o What's going on here?
11 Answers
+ 1
@Saphire try swapping const with constexpr to see if that works?
0
Given the code you have shared, it should be fine. Please share the definition of item.
0
Item definition shouldn't be relevant:
typedef struct {
item_tag tag;
union {
int i;
double d;
string s;
} value;
} item;
It works just fine, as it's been used hundreds of times before. However, the size of the array simply won't accept a variable in place of a literal. For example, I can tell the array that it should be 1024, and it won't complain. But if I use a variable in its place, a constant at that, it won't accept it.
0
typedef char * string;
typedef enum { INTEGER, DOUBLE, STRING } item_tag;
For reference
0
I'm just as confused. Stack size is only declared once, under the header file, which is also surrounded by #IFDEF statements. So I got no clue why it's complaining.
Perhaps someone has encountered this before and can shed some light, if not, tomorrow I'll ask one of the profs.
0
It's a c file, not c++. I've used a macro in its place, it doesn't seem to be complaining now, but that's something I haven't encountered before.
0
won't we get any playground here to run Swift code
- 1
Did you recently add the string s; line? I'm getting compile errors on it.
- 1
I found my error and it compiles fine on my system, now. There isn't a difference between MAX_STACK_SIZE and 1024 for the stack definition. I have never seen this type of issue in my 33 years of C++ experience. I suggest putting #ifdef in to ignore parts of the source file to see if it goes away. There might be something causing the compiler to get confused.