|
Class not working |
Vanneto
Member #8,643
May 2007
|
So I have this basic class to draw buttons: And here is the file i use it in: // Including files here // main() and allegro init stuff... DATAFILE *data = load_datafile("mydata.dat"); Button *b; b->draw_button(screen, data[button_sometin].dat, SCREEN_W/2, SCREEN_H/2); // ... I get these errors. And while were at it, whats the diffrence between object :: sometin, object . sometin and object -> sometin. Thats all. Just tryin' to figure out the basics. I appreciate all help I get! Thank you! In capitalist America bank robs you. |
X-G
Member #856
December 2000
|
You obviously forgot to cast data[blah].dat to a BITMAP*. -- |
Vanneto
Member #8,643
May 2007
|
Well.. Yeah now its obvious! Thank You! What about the diffrent operators? (:: , -> , .) Whats the diffrence between them? P.S. Thanks for the quick reply, works fine now! In capitalist America bank robs you. |
X-G
Member #856
December 2000
|
Uh... they do completely different things. -- |
LennyLen
Member #5,313
December 2004
|
Quote: What about the diffrent operators? (:: , -> , .) Whats the diffrence between them? :: is the scope operator. -> is basically a combination of * and . a->b == (*a).b
|
Vanneto
Member #8,643
May 2007
|
I really need to brush up the OOP skills. I suck at this. (too much PHP OOP ).
Yeah I know its not good. But its a start. I also got this in the main file: It compiles fine, but then when I run it. It crashes. I dont know why. Its the "FILENAME.EXE has encountered an unknown error" problem. Anyone know what to do? Thanks! In capitalist America bank robs you. |
Johan Halmén
Member #1,550
September 2001
|
Object Oriented Programming Skills ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. |
X-G
Member #856
December 2000
|
Let's see the actual code for it. Obviously there has to be more to it, since you don't even load data with anything in that snippet; so let's see all of it. -- |
Vanneto
Member #8,643
May 2007
|
This is it. The class is in my previous post. I holp it will help! Thanks! In capitalist America bank robs you. |
LennyLen
Member #5,313
December 2004
|
Are you even certain the datafile is being loaded? I see no return values being checked. edit: Ok, I'm blind. edit2: where is print_error() defined?
|
Vanneto
Member #8,643
May 2007
|
Ups. I only copied the class Button include. Now all the headers are there. Sorry about that. Its defined in printer.h, its basically an textout_ex() function but automatically sets RED color and position to center... slh.ALL. propDATE 5-20-2007, 0:10propNAME start_buttonpropORIG ,d:\CDEV\bitmap\button_start.bmppropXPOS -1propXSIZ -1propYPOS -1propYSIZ -1BMP @ propDATE 5-20-2007, 0:09propNAME cursorpropORIG &d:\CDEV\bitmap\cursor.bmppropXPOS -1propXSIZ -1propYPOS -1propYSIZ -1BMP propBACK npropDITH npropHNAM datafilepropNAME GrabberInfopropPACK 0propRELF npropSORT npropTRAN npropXGRD 16propYGRD 16info For internal use by the grabber Hmm.. Maybe heres the problem. When I used the grabber I was getting the bitmaps from bitmap/ folder and I saved the datafile to the main folder ( / ). I think its a bit small... And no data is there. Only the paths to the data is there! EDIT: I tried this: start_b->set_bitmap(load_bitmap("button_start.bmp", NULL)); Doesent work either. Dont know why. Its a simple class function. The code is a few posts above! In capitalist America bank robs you. |
X-G
Member #856
December 2000
|
unload_datafile(data); <-- How do you expect the bitmap to still be available if you immediately unload the datafile again? EDIT: Check your return values, too. Make sure you're not getting nulls back somewhere (I bet you are, though). -- |
Vanneto
Member #8,643
May 2007
|
I unload it after its been assigned to the class function. Yeah thats possible. Ill check for that. Thanks for the advice! In capitalist America bank robs you. |
LennyLen
Member #5,313
December 2004
|
Quote: start_b->set_bitmap(load_bitmap("button_start.bmp", NULL)); Load the bitmap earlier, so that you can check it is being loaded, then pass it to your function.
|
X-G
Member #856
December 2000
|
Quote: I unload it after its been assigned to the class function. Yeah, and what do you think happens to the bitmap when you unload it? That's right, your pointer turns into a dangling pointer and when you try to use it, you crash. -- |
Vanneto
Member #8,643
May 2007
|
I did that. And now I know the problem is definitely in the .dat file. Now there are some possibilites why it doesent work. a) My grabber.exe is corrupt, b) I dont know how to use it and c) Im an idiot. Gonna make some new bitmaps and try again! :p EDIT: I am currently making bitmaps with PS CS2. I draw something. And then Save As... / Bitmap (.bmp) / 16 bit mode / Save. Is this right? Or is there better way? In capitalist America bank robs you. |
X-G
Member #856
December 2000
|
That should be fine. I don't want to sound condescending - really - but b) is probably the most likely answer. Though it's been a long time since I used the grabber myself, so I'm probably no better myself. -- |
Vanneto
Member #8,643
May 2007
|
Sorry about that one. Its definetely the class causing the problem. Lets forget the rest of the code. I have this class:
Ok, all well and fine. And then in the main file: Button *BUTTON_START; Button *BUTTON_QUIT; BUTTON_START->set_bitmap((BITMAP*)data[start_b].dat); BUTTON_START->set_coords(CENTER_W, CENTER_H + 20); Now this causes the crash. Because if I comment out ..->set_bitmap() and ->set_coords everything is fine. So the problem IS in the class. Now whats wrong. I cant see the problem. When I use set_bitmap the specified bitmap parameter is assigned to the class variable. Nothing to it. But why is it causing a crash? Or is something else causing it... Anyway to solve this? Its the only thing stopping me from going on. P.S. This is definetely the part of code causing the problem. The rest of the code is located a few posts up. And no, the DATAFILE is not the culprit. I blipped the contents of it on the screen and it worked... Thank you! In capitalist America bank robs you. |
gnolam
Member #2,030
March 2002
|
And where are you instantiating your classes? In the future, post the smallest complete code that exhibits the problem... -- |
Vanneto
Member #8,643
May 2007
|
Button *BUTTON_START; Button *BUTTON_QUIT; Doesent this initialise the classes? Button *BUTTON_START; Button *BUTTON_QUIT; BUTTON_START->set_bitmap((BITMAP*)data[start_b].dat); BUTTON_START->set_coords(CENTER_W, CENTER_H + 20); This causes the problem. The rest of the code it in earlier posts. I thought classname *objectname initialises the class? Thanks! In capitalist America bank robs you. |
ReyBrujo
Moderator
January 2001
|
No, when you have a pointer, you need to make it point to somewhere. Basically, your code is: Button *BUTTON_START = 0xDEADBEEF; Button *BUTTON_QUIT = 0xFEEDF00D; In other words, unless you point the pointers somewhere, they are pointing to trash, and when you try to execute trash with a BUTTON_START->set_bitmap, if you are lucky your game crashes horribly and closes. (Edited: Try Button *BUTTON_START = NULL; Button *BUTTON_QUIT = NULL; BUTTON_START = new Button; BUTTON_QUIT = new Button; (Edited 2: Oh, PHP? Welcome to memory hell then ) -- |
Vanneto
Member #8,643
May 2007
|
Heh thanks. Im already getting headaches from having to deallocating stuff etc. :S D:\CDEV\space_invade\main.o:main.cpp:(.text+0x430): undefined reference to `Button::Button()' D:\CDEV\space_invade\main.o:main.cpp:(.text+0x489): undefined reference to `Button::Button()' And it doesent compile. Probably my fault. Just checking. Thanks! In capitalist America bank robs you. |
ReyBrujo
Moderator
January 2001
|
You haven't defined the constructor and destructor in your class. Temporarily, replace Button(); in your class with Button() { } and ~Button(); with ~Button() { } (or just delete them altogether), and it should compile. Note that your constructor should set the bitmap member to NULL, and the destrutor, destroy it, and you should call delete BUTTON_START before finishing Allegro, but for now that should work. If you get a bunch of undefined Allegro stuff, you forgot to add a reference to Allegro in the linking section of the IDE. -- |
Vanneto
Member #8,643
May 2007
|
I added the -lalleg already. And BTW: IT WORKS!!! Thanks. Something so simple (well, at least now) and changes so much. Thank you very much. This language is causing me so much agony at the moment.. Thanks for all the support from all the people who replied to this thread and helped! And especially thank you ReyBrujo! THANKS! In capitalist America bank robs you. |
gnolam
Member #2,030
March 2002
|
Now that the problem is resolved, I must add the following comment: Quote: Class not working I guess it's bourgeoisie then. -- |
|