multidimensional arrays... redeclaring

Started by
6 comments, last by Zakwayda 18 years ago
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
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend
Advertisement
gel (*board)[10];board = new gel[10][10];
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I need it to resize ;-)
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend
Is this C or C++?
C++ ... and if it helps I'm writing it on BSD using G++.
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
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.


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 :-)
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend
Quote:Original post by Fruny
jyk -- he's using new, it can't be C.
Yup, didn't read the original post carefully enough - my bad.

This topic is closed to new replies.

Advertisement