I am very new to C++ so please forgive my ignorance and ineptness.
I am trying to create a class called Planet. Every planet will have a width and height (they are stored as rectangles because I'm not a complete masochist). Different planets have different widths and heights.
The class therefore needs member variables to store these values. It also needs a number of arrays to store terrain information and the like. The size of these arrays should be determined by the value of the width and height variables. So each object would have different-sized arrays. My problem is: how can I declare these arrays within the class?
Trying to declare arrays using member variables simply doesn't work:
class planet
{
public:
planet(); // constructor
~planet(); // destructor
// other public functions go here
private:
int width; // width of global map
int height; // height of global map
int terrainmap [width][height];
};
This causes the error "Invalid use of non-static data member 'height'", which makes sense as obviously the compiler doesn't know how big that array should be. This also applies if I make them static variables.
I have tried doing it with vectors instead, since they are more flexible:
vector<int> terrainmap[width][height];
But I get exactly the same error.
I suppose I could just initialise an array or vector with the largest possible values for width/height, but that seems wasteful if some objects in this class will have smaller values and therefore won't be using the whole array. Is there an elegant solution to this?