I'm trying to write a simple game. I made a class for the player object, and allocated the memory for the health and points dynamically so I could delete them later (no practical purpose, I just wanted to practice doing that). Unfortunately, VSC++ has other ideas. Here's the relevant code:
1 2 3 4 5 6 7 8 9 10 11 12
class player{
int * health;
int * points;
player(){ //player constructor//
health = newint;
points = newint;
points = 0;
health = 100; //this is what the compiler complains about. Error message below.
}
}P1;
So the error message is this:
cannot convert from 'int' to 'int *'
Oddly enough, it only says this about line 27, where I say health = 100. It says nothing about me assigning 0 to points, which is the exact same operation. I don't even understand. health isn't an int* anymore, so what is it even talking about? Is it just a screwed up compiler, or am I missing something?
You're forgetting to dereference points and health before assigning the values. And yes, health is an int*. You declare it to be an int*, and nothing you've done in that bit of code changes that. :)
Also, your code has a memory leak. Are you forgetting a pair of deletes somewhere?
Thanks. I was under the impression that the new changed a pointer into a regular variable when it made it into dynamic memory. I do have delete for each variable, in the destructor, but i removed it to keep the code relevant. Thanks a lot : ) I'm glad it was I who was wrong, and not the compiler.