multidimensional arrays... redeclaring
I have an array of objects, made from a class in my project. array is as follows:
gel board[0][0];
but thats just to create it ... i need to set its size afterwards ... i am trying to do so like this:
board = new gel[10][10];
which obviously isnt right because the compiler gives errors! what is the correct way to do this?
error message follows...
./minesweeper.cpp: In constructor `Board::Board(int, int, int)':
./minesweeper.cpp:52: error: incompatible types in assignment of `gel (*)[10]' to `gel[0u][0u]'
./minesweeper.cpp:56: error: cannot convert `gel' to `gel*' in initialization
Quote:Original post by xyuri
I need it to resize ;-)
Only the first dimension can be dynamic. Consider using a 1-dimensional (row_size*col_size) array and manually computing the appropriate index as:
index = column + row_size * row;
Or you can use the boost::multi_array class, or a dynamically allocated array of dynamically allocated arrays (but that's more trouble than it's worth).
std::vector is also a very good choice to take care of the array itself. Especially when resizing is involved.
jyk -- he's using new, it can't be C.
Quote:Original post by FrunyQuote:Original post by xyuri
I need it to resize ;-)
Only the first dimension can be dynamic. Consider using a 1-dimensional (row_size*col_size) array and manually computing the appropriate index as:
index = column + row_size * row;
Or you can use the boost::multi_array class, or a dynamically allocated array of dynamically allocated arrays (but that's more trouble than it's worth).
std::vector is also a very good choice to take care of the array itself. Especially when resizing is involved.
jyk -- he's using new, it can't be C.
aaaah... only the first dimension... that explains some of the other error messages as well :-) yeah, might just go with a single dimension then. Thank you very much for your assistance :-)
Quote:Original post by FrunyYup, didn't read the original post carefully enough - my bad.
jyk -- he's using new, it can't be C.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement