Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Arrays and creating objects

Author
Message
Kurt
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location:
Posted: 6th Mar 2008 22:11 Edited at: 6th Mar 2008 22:15
Ive been sitting here a long time now and I just CAN'T get this problem solved:

object.h:


object.cpp



load.cpp:




use.cpp



The problem is, "obj.objects[1];" returns something very strange. Something like 1,596233e132. I know what that means though I have no clue what that means according to my array. I simply want it to get back "1", as I assigned. Any ideas? My head is about to fall off!
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 7th Mar 2008 05:17
Learn C++. You're trying to use a Static Array like a dynamic One. C++ trusts you... and right now it shouldn't

Don't be discouraged.. check out [href]www.cplusplus.com/docs/tutorial/[/href] and check out STDLIB "Vectors" a main stream dynamic array library many use.

alternately ... Dig into the OOP DarkGDK thread on here and there are various source snips and I myself release literally thousands of lines of code to peruse, demos to run, and most importantly learn from.

Hang In there... glad you're here... Don't let your head fall off... I heard in some cases you can die from that... Cockroaches having the highest percent of survivors after this catastrophe but I read eeven they die once they starve from not having a mouth any longer.

Killer Fish
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location:
Posted: 7th Mar 2008 06:18
Well, I went through your code and tried to fix a few things. I think I got it to work in the end. The major problem, as far as I know, was that every time you went through LoadCharacter (void), resetx (void), createObject (void), or checkItem (void), you redeclare an object of the class makeObject under a currently used name. You already created an object of the class makeObject called obj after you defined the class.
I am still a beginner to C++, and I'm not sure if this is right, but it almost seems like every time you make an object called obj, it overwrites the old one. Another change I made, because I don't know the difference between "extern class" and "class", and because my compiler got errors with "extern class" that I didn't know how to fix, I switched it to a class.
However, I never got the same problem that you had. For me, obj.objects[1], either returned a 0 or a 1. Anyway, I'll post the revised snippets here. See if they work for you.

Your object.h


Your object.cpp


Your load.cpp


Your use.cpp


Please try these and let me know what happens. I'll try to get back to this and see what happened for you. If you need anymore help, let me know and I'll try to help. Remember though, I'm a beginner still to C++!
By the way, I noticed that you use objects[1] but not objects[0]. In C++, arrays begin at 0, just in case you didn't know.
I hope this helps!
Kurt
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location:
Posted: 7th Mar 2008 07:15
Well thanks guys. After debugging a little, I came up with the same thing. The array is nulled every time i call the function. Luckily, my father is a former programmer in C++(right now he's java, so he's forgetting stuff) so he gave me some hints of what to do. He told me to use pointers called next instead of arrays(at least as I understood). So that the pointers point to another object. Right now, I have no idea what a pointer is. So, I'll look up what it is, try to make one and then I'll post it if here if there is any intrests?

Btw, this is my first program in C++, so please don't judge me.... Jet


THanks for all the help
monotonic
18
Years of Service
User Offline
Joined: 24th Mar 2006
Location: Nottinghamshire, England
Posted: 7th Mar 2008 12:00
Quote: "He told me to use pointers called next instead of arrays(at least as I understood). So that the pointers point to another object"


I believe he is talking about dynamically linked lists. There is one ready made for you in C++ (STD::LIST<>. But, yeah linked lists are used for dynamic memory allocation.

Do a google search for 'std::list' and it will come up with all kinds of bountiful information. Although, I found writing my own quite a good learning experience back when I was learning C++.

Much good work is lost for the lack of a little more.
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 7th Mar 2008 13:04
Note: I just rewrote my C++ OOP library of code to really minimize the number of linked lists because they are not as fast as looping through an array. Though they are very usuful to... but in a game - you want faster usually.

Example - All my objects and stuff are tracked via arrays of classes, same for almost everything HOWEVER... My Speed Gauge mechanism... has a small list of "last speeds" to average the MPH... and my Double Linked list class is great because I can delete the first item and append a new item then perform the averaging really easily.... No impact on frame rates.

Having Dynamic double linked lists everywhere though added over head... as the navigation through the list takes more cpu then just flipping through an array.

Login to post a reply

Server time is: 2024-11-17 08:54:47
Your offset time is: 2024-11-17 08:54:47