zuzurino zuzurino - 10 months ago 46
C++ Question

Initializing a member array's size with another member within the class

Here is my code

class world{
int w;
int h;
int windowWidth = 80;
int windowHeight = 46;
char windowMap[windowHeight][windowWidth];

I want to make the size of windowMap[46][80] by using windowWidth and windowHeight but I get this error

warning: non-static data member initializers only available with
-std=c++11 or -std=gnu++11

is there a way to fix this or a better way of achieving the same results?

Answer Source

The variables for width and height must be constant and static, since they should be available during compile time. Otherwise, you could use a vector and allocate the memory dynamically in the constructor. Then you could specify the size of the windowMap as arguments, thus making the class more reusable.

The simple changes are:

const static int windowWidth = 80;
const static int windowHeight = 46;