I'm coding a game in Allegro, but this problem seems to go beyond that. Whenever I create an object, the constructor doesn't seem to be setting the values of member variables that it should be. Besides that, when I call member functions to alter member variables, the same happens. I don't understand at all. Please help. I've gotten to the "banging my head on the wall" stage.
Class Comet{
public:
float x;
float y;
int width;
int height;
ALLEGRO_BITMAP * bitmap;
float speed;
Comet(float xVal, float yVal){
x = xVal;
y = yVal;
speed = 10;
width = 10;
height = 10;
bitmap = al_load_bitmap("comets.png");
}//end of constructor
};//end of Comet class
Comet comet(5.0, 5.0);
int main(void){
/*For now I've decided to use semi-psuedo code. Normally there would be
a lot more code prior to and after the draw function, but my problem
arises when I try to draw the bitmap and nowhere else really*/
al_draw_bitmap(comet.bitmap, comet.x, comet.y, 0);
return 0;
}//end of main
Now the above code will yield a runtime error. I'm in MSVC++ Express 2010 and it tells me that comet.bitmap = 0x0000 when I call this function (so basically the al_load_bitmap() function returned a NULL pointer--oh and yes, the file comets.png file is where it's supposed to be. If I type out comet.bitmap = al_load_bitmap("comets.png"); in main, it loads the image), and x and y both equal some weird negative number which indicates they haven't been initialized. So could you please explain what I'm doing wrong, if you have any idea? Thanks so much for any help you can give me.
Well, apart from some errors that I fixed that seemed unrelated to the problem (should be class, not Class and width/height don't exist in your class) it works fine for me.
Well I knew there'd be some dumb errors in there; that's a severely pared down version of the code. Anyway maybe it has something to do with the type of file I'm saving the classes in. It's a header file (as in .h file extension). My main.cpp includes that header. Should it be a cpp file or something else other than .h?
EDIT:
So I've copy/pasted everything from my header into my main.cpp and removed the #include for the header, and now the code works. So I guess I've got to learn more about what exactly a header file is compared to other files. Or I'm doing something else wrong. Either way, I'm doing something wrong. Just an update for anyone else who has some info to add, or anyone else having this problem.
One final update on the subject. I just realized the difference between the posted code and my code is that I was initializing my object in main, so that it didn't have any scope in my game loop, which is its own separate function. As a result, when I tried to access the data it stored (typically via pointers) the pointers weren't actually pointing to anything.