Search code examples
c++arraysconstants

Why can my constant not be used to create my array?


I want to make an array that holds a number of tile objects (called Tile) but it's saying that my constant int, numTiles, cannot be used as a... constant int... what? The code I'm having issues with is here (error on line 5):

// Variables - Tilemap
const int tileSize = 32;
const int screenWidth = GetScreenWidth();
const int numTiles = screenWidth / tileSize;
Tile tilemap[numTiles];

The error I get is as follows: expression must have a constant value the value of variable "numTiles" (declared at line 14) cannot be used a constant

I'm not sure why I'm getting this issue as it looks like numTiles is definitely a constant int... Can someone explain what the problem is here?


Solution

  • Thanks to the people in the comments of the post for explaining this to me: The variables are calculated at runtime when they need to be calculated at compile time (this was due to my usage of GetScreenWidth(), which obviously cannot run during compilation).