-
Try it out. Looks good.
Note, you have only allocated memory for 5 of the possible 10 elements in Anthony.Location. If you try doing this:
Anthony.Location[5][1].setDescpription("BOO!");
your program may well behave erratically or, if you're lucky, crash. To fix this potential problem, look more closely at Salem's code.
-
OHHHHHHHHHHHHHHH. I SEE THE ERROR NOW :)) LOL I accidently put the loop for 5 not 10
THANKS :D
-
I believe it should be like this:
Code:
Anthony.Location = new Space*[10];
for( int i=0;i<10;i++){
Anthony.Location[i]=new Space[5];
}
//...
for( int i=0;i<10;i++){
delete [] Anthony.Location[i];
}
delete [] Anthony.Location;
Also, your class design isn't all that great. Any data in a class should generally be declared with private scope, and then you should declare a constructor and destructor to allocate/deallocate memory:
Code:
class Player
{
public:
Player(int r=10,int c=5);
~Player();
private:
Space** Location;
int rows,cols;
};
Player::Player(int r, int c)
{
rows=r;
cols=c;
Anthony.Location = new Space*[r];
for( int i=0;i<r;i++){
Anthony.Location[i]=new Space[c];
}
}
Player::~Player()
{
for(int i=0;i<rows;i++){
delete [] Anthony.Location[i];
}
delete [] Anthony.Location[i];
}
You should probably review a tutorial on classes before you move on :)
Edit: Whoops missed the [] in the second part
-
:D I Know my Class isn't that Great. Im Just Tring to get the Parts working before I start organizing it better. But then again Im just a beginner. So It wasnt going to get much more better then it was without help LOL. :D Thanks for the Help. The Correct way to Delete really Helped. Also I would change your code a tad. So that Space handles all the Memory Allocations and stuff. And Addings new levels. I liked the new additions with the constructors. I need to learn more on classes I agree. However I think Coding and learning as I go is helping me a whole lot. Im not a real good learner when it comes to reading. And then It is hard for me to remember the things I learn on my way LOL. Thanks for all the good advice. :D