C++ Array Size Initialization -
i trying define class. have:
enum tile { grass, dirt, tree }; class board { public: int toshow; int tostore; tile* shown; board (int tsh, int tst); ~board(); }; board::board (int tsh, int tst) { toshow = tsh; tostore = tst; shown = new tile[tostore][tostore]; //error! } board::~board () { delete [] shown; } however, following error on indicated line -- first dimension of allocated array can have dynamic size.
what want able rather hard code it, pass parameter toshow constructor , create two-dimensional array contains elements want shown.
however, understanding when constructor called, , shown initialized, size initialized current value of tostore. if tostore changes, memory has been allocated array shown , therefore size should not change. however, compiler doesn't this.
is there genuine misconception in how i'm understanding this? have fix want without having hard code in size of array?
use c++'s containers, that's they're there for.
class board { public: int toshow; int tostore; std::vector<std::vector<tile> > shown; board (int tsh, int tst) : toshow(tsh), tostore(tst), shown(tst, std::vector<tile>(tst)) { }; }; ... board board(4, 5); board.shown[1][3] = dirt;
Comments
Post a Comment