Problem with pointer to multidimensional array on heap.
Hello, I''m pretty new to programming c++ and could use some help with a problem I''ve run into. I''m trying to do a tic-tac-toe (I think that''s what it''s called in English) DOS game and what to use a pointer to an array of an enum that represent the board. If have defined it as following:
COLOR (*Board);
And then I try to initialize it:
Board = new COLOR[*iRows][*iCols];
for(int i = 0; i < *iRows; i++)
for(int j = 0; j < *iCols; j++)
*Board[j] = new COLOR(EMPTY);
When I compile it I get these errors
1) ''='' : cannot convert from ''enum COLOR (*)[3]'' to ''enum COLOR *''
2) binary ''['' : ''enum COLOR'' does not define this operator or a conversion to a type acceptable to the predefined ope
rator
What is it I have done wrong?
Any help appreciated, thx.
Try something like this:
COLOR **array;
array = new COLOR*[iRow];
for (int i = 0; i < iRow; i++)
array = new COLOR [iCol];
my int''s are int*''s. You''ll have to change that for yours.
COLOR **array;
array = new COLOR*[iRow];
for (int i = 0; i < iRow; i++)
array = new COLOR [iCol];
my int''s are int*''s. You''ll have to change that for yours.
Is there no way to create a pointer to a multidimensional array on the free store rather than creating a pointer to a single dimensional array of pointers that are each pointers to single dimensional arrays on the free store. Not that it doesn''t work. I just think it seems a bit far-fetched.
quote:Original post by TobiasS
Is there no way to create a pointer to a multidimensional array on the free store rather than creating a pointer to a single dimensional array of pointers that are each pointers to single dimensional arrays on the free store. Not that it doesn''t work. I just think it seems a bit far-fetched.
No there isn''t. That''s how you create a 2 dimensional array dynamically. Once it is created though, you can access it just like a normal 2 dim array.
---
Make it work.
Make it fast.
"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home
Yes there is, you can allocate a single buffer that is large enough to hold the pointer-map and the actual data - then cast everything into place. RAM is RAM.
Creating an array of an array of pointers is straight forward, but inefficent.
If the size doesn''t need to change at run-time, you can use a template.
Creating an array of an array of pointers is straight forward, but inefficent.
If the size doesn''t need to change at run-time, you can use a template.
Heh, never thought of it even though it's a pretty obvious solution. I'll probably be using that instead. Thanks Magmai Kai Holmlor for that valuable piece of info!
Anyway, I've run into another problem with the early solution. Here's the code I use:
At line 8 I'm giving Board[j] the value EMPTY. Originally I wrote *Board[j] but got a compiler error basically telling me to remove the asterisk. Since you're not supposed to give the address the value the only conclusion I can come up with is that the second dimension is not really a pointer but rather just a normal variable of type enum on the stack. Please explain what I'm missing here?<br><br>[edit: source tags, and (without the spaces) means italics]<br><br><br>Edited by - Magmai Kai Holmlor on February 1, 2002 7:29:21 PM
Anyway, I've run into another problem with the early solution. Here's the code I use:
CBOARD::CBOARD(int rows = 3, int cols = 3){ iRows = new int(rows); iCols = new int(cols); Board = new COLOR*[*iCols]; //declare and initialize tic-tac-toe board for(int i = 0; i < *iCols; i++) { Board[i] = new COLOR[*iRows]; for(int j = 0; j < *iRows; j++) Board[i][j] = EMPTY; }}
At line 8 I'm giving Board[j] the value EMPTY. Originally I wrote *Board[j] but got a compiler error basically telling me to remove the asterisk. Since you're not supposed to give the address the value the only conclusion I can come up with is that the second dimension is not really a pointer but rather just a normal variable of type enum on the stack. Please explain what I'm missing here?<br><br>[edit: source tags, and (without the spaces) means italics]<br><br><br>Edited by - Magmai Kai Holmlor on February 1, 2002 7:29:21 PM
Side question: How do you create a inline code window?
didn''t seem to be the right approach.
quote:
iRows = new int(rows);
You should ~*NEVER*~ need to do that in C/C++.
Just set iRows = rows; and make iRows an int, not a pointer to an int.
If it's a Tic-Tac-Toe board, just hard set it to [3][3]
Edited by - Magmai Kai Holmlor on February 1, 2002 7:33:13 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement