Problems with Pointers

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
struct Point {
	double x;
	double y;
};

enum SpaceObjType { SHIP, ASTEROID, PHOTON_TORPEDO }; 

//=============================================
class SpaceObject {

public:
	SpaceObject();
	SpaceObject(SpaceObjType type);

private: 
	SpaceObjType type;	  //type of object
};
SpaceObject::SpaceObject(){
    type = ASTEROID;
}

//=============================================
SpaceObject::SpaceObject(SpaceObjType type){

        this->type = type;
}

//=============================================
int main() {

    SpaceObject asteroid(ASTEROID);
    SpaceObject * ship = SpaceObject(SHIP); //line with error 
    /*
    no suitable conversion function from "SpaceObject" to "SpaceObject *" exists
    no viable conversion from 'SpaceObject' to 'SpaceObject *'
    */

    return 1;
}
Well it appears that you're trying to assign a "normal" object to a pointer. Is there a reason you're trying to use a pointer in line 32?

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?

In short you forgot your 'new' operator. That's it.
http://www.cplusplus.com/doc/tutorial/dynamic/
SpaceObject * ship = new SpaceObject(SHIP);

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.
Thank you Grime, this solved the issue.
And don't forget 'delete' after

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.

Topic archived. No new replies allowed.