Pointer/Array Error
I have a bool pointer. When I define it using the new operator, I get an error. Here's the code (int Size is defined earlier):
bool* Path;
Path=new bool[Size][Size][Size];
The error says something about an illegal constant expression, or something like that.
I haven't programmed a dynamic array in a while, so I can't remember the correct syntax, but I thought this was correct.
--------------------------------------
I am the master of stories.....
If only I could just write them down...
[edited by - Nathaniel Hammen on March 21, 2003 11:56:29 AM]
I am the master of ideas.....If only I could write them down...
You can''t allocate multi-dimensional arrays using new in a single line like that.
[twitter]warrenm[/twitter]
Okay, so how would I go about doing something similar?
--------------------------------------
I am the master of stories.....
If only I could just write them down...
--------------------------------------
I am the master of stories.....
If only I could just write them down...
I am the master of ideas.....If only I could write them down...
I''d look into using linked lists for what you''re trying to do - much easier to manage and work with. Any decent C/C++ book should have plenty of examples for using linked lists - insertion, deletion, searches, editing data. Model a linked list that works like a 3D array - and it''ll also be expandible.
(silencer)
(silencer)
Use the following code:
I think that will do it...
-Auron
// To instantiatebool ***Path;int Size1, Size2, Size3, ctr;Path = new bool** [Size1];for (ctr = 0; ctr < Size1; ctr ++){ Path [ctr] = new bool* [Size2]; for (int ctr2 = 0; ctr2 < Size2; ctr2 ++) Path [ctr][ctr2] = new bool [Size3];}// To deletefor (ctr = 0; ctr < Size2; ctr ++){ for (int ctr2 = 0; ctr2 < Size2; ctr ++) delete [] Path [ctr1][ctr2]; delete [] Path [ctr1];}delete [] Path;
I think that will do it...
-Auron
March 21, 2003 11:16 AM
Or if you want an alternative solution, you can just transform the indices so as to map your three dimensional array onto a one dimensional array - the maths isnt that difficult
March 21, 2003 11:22 AM
using
bool *pSth=new bool[size*size*size];
then use
pSth[m*size*size+n*size+o]
to access element at[m][n][o]
bool *pSth=new bool[size*size*size];
then use
pSth[m*size*size+n*size+o]
to access element at[m][n][o]
I was just thinking about doing something like that, Auron. Thanx for the help. Since, I have already been thinking about that I already know basically how that will work. That will be pretty simple.
Anonymous Poster, whoever you are, that would work good as a templated class, with an overloaded bracket operator. I don''t want to take the time to do that though.
Thank you everyone, I appreaciate the help.
--------------------------------------
I am the master of stories.....
If only I could just write them down...
Anonymous Poster, whoever you are, that would work good as a templated class, with an overloaded bracket operator. I don''t want to take the time to do that though.
Thank you everyone, I appreaciate the help.
--------------------------------------
I am the master of stories.....
If only I could just write them down...
I am the master of ideas.....If only I could write them down...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement