In my code I'm trying to change the ship object variable into a pointer variable. The ship variable in main originally looked like the asteroid variable above it. I was wondering how you how you make the change between object variable and pointer variable. Did I initialize wrong? Am I just missing a line of code to make it work? I stripped down the code to try and make it easier to read. Any help figuring out the problem is much appreciated.
I'm trying to dynamically allocate the the memory for the ship so that I can manipulate it and delete it later. The entire program is suppose to be a recreation of asteroids. I want to be able to assign the objects to pointers so when I no longer need them I can delete them and free up memory and not have a leak.
Okay, so how do you manually allocate memory for the ship? Do know that if you remove that pointer the program would probably compile without error and with no worry about memory leaks?
The entire program is suppose to be a recreation of asteroids.
Okay, and what exactly are the rules and strategy of asteroids?
I want to be able to assign the objects to pointers so when I no longer need them I can delete them and free up memory and not have a leak.
Okay, how do you free up memory you no longer need and not have a leak? What exactly do you mean by a leak? Do you know that the best way to not have a memory leak is to not use dynamic memory in the first place?
And don't forget 'delete' after, or jib will seriously butcher you.
Also in main, you've written return 1;. Either you don't write a return statement at all (this is only for main function) or you write return 0;return 0; is the convention. 0 is for when the program ran into no errors.
I agree that removing the pointer will allow the code to compile without error. The code originally did not contain pointers at all. However, the final goal is to manipulate the memory of the objects ( SHIP, ASTEROID, PHOTON_TORPEDO ) so that when they get destroyed or disappear from the current game the memory for that object isn't lost to the heap. The final game should be able to run indefinitely with out a stack overflow error caused by having created too many asteroids. I'm quite a newbie to c++, so I hope what I'm writing conveys why I'm trying to turn the objects into pointers variables.
Actually I would prefer the use of smart pointers instead of the raw pointer. Also if you must use raw pointers, you really should encapsulate those pointers into a class that takes care of allocating and deleting that memory.