I have the following code in 3 files:
Defines.h
#ifndef Defines_h
extern const unsigned int SIZE;
#endif
Defines.cpp
#include "Defines.h"
const unsigned int SIZE = 10;
Main.cpp
#include "Defines.h"
int main()
{
int x[SIZE] = {0};
}
On compiling I get the error at the array definition line as:
error C2057: expected constant expression and
C2466: cannot allocate an array of constant size 0
Why does this happen, after all I have SIZE which is indeed a constant ?
Move
const unsigned int SIZE = 10;into the header and remove the extern line. With a simple const int value it is safe and harmless to declare it directly in-header.