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 / Array of Classes inside a class

Author
Message
The Kurgan
16
Years of Service
User Offline
Joined: 8th May 2008
Location: At the Beginning
Posted: 1st Jun 2008 07:45
Well, I managed to make an array of a class inside a method, but when I tried to move it into another class, to make it easier to refer to outside of that method it will compile but not run. Is there a way to store an array of classes inside a class?

Here is my code, the relevant bits:


"It's better to burn out, than to fade away!" -- The Kurgan
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 1st Jun 2008 08:30
You seem to be doing it right, so what do you mean "won't run"?

Basically to access it you'd have something like:

Matrix KeaneauReeves;

KeaneauReeves.thePlatforms[27].sizeX = 33.3f;

The problem I see is that you'd have some problem using that rather extended constructor. Each instance of thePlatforms[x] will be constructed when an instance of Matrix is created. Since you can't pass specific data to each instance it will take use the constructor without the parameter list.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
The Kurgan
16
Years of Service
User Offline
Joined: 8th May 2008
Location: At the Beginning
Posted: 2nd Jun 2008 05:35
Any time I try and run the executable, it crashes before anything useful happens.


And I ran into that problem earlier; I THOUGHT I had included the no arguments constructor in my code snippet, but I do have one.

"It's better to burn out, than to fade away!" -- The Kurgan
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 2nd Jun 2008 05:42
Try a debug session to try to determine where it breaks down.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Bishop
21
Years of Service
User Offline
Joined: 18th Dec 2002
Location: In my favorite chair...
Posted: 2nd Jun 2008 10:54
Only two problems I can see; Firstly, you're declaring a constructor BEFORE your default constructor. Try declaring the Default and then the Custom one.

Secondly, you're declaring a custom constructor, but not defining it. Once you get your constructors in the right order, make sure you define them.


Tux is my guildmaster.
The Kurgan
16
Years of Service
User Offline
Joined: 8th May 2008
Location: At the Beginning
Posted: 3rd Jun 2008 04:23
"Defining?"


What exactly do you mean by that?

"It's better to burn out, than to fade away!" -- The Kurgan
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 3rd Jun 2008 04:41
Honestly - we need more code to help - Lilith is right - step through it - I bet you're using the class before a new or something...

Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 3rd Jun 2008 04:43
Bishop is saying that you're showing a constructor with arguments but it's not apparent that you've actually telling the compiler (defining) what to do with that information. That shouldn't be a problem because, at least in the code you've provided, you've made no calls that requires it.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
The Kurgan
16
Years of Service
User Offline
Joined: 8th May 2008
Location: At the Beginning
Posted: 3rd Jun 2008 07:47
Hmmm.

Thanks for the debugging tip, that helped me figure it out...


seems, for whatever reason, that the "Platforms" type interferes with my "Matrix" type, to the point that it forgets the values of variables...
It was dividing by zero (because of that) and once I replaced a few variables with constants it worked.

"It's better to burn out, than to fade away!" -- The Kurgan
Bishop
21
Years of Service
User Offline
Joined: 18th Dec 2002
Location: In my favorite chair...
Posted: 3rd Jun 2008 13:50
Indeed, there shouldn't have been any problems because you had no call for it, and said call would have been made at runtime. In any case, it is good practice to define any functions you declare.

In C++, when you make a function prototype, this is called "declaring". Then, whenever you decide to flesh out the function and tell the compiler what to do when it's called, is called "defining".

Get in the habit of defining a function whenever you declare one. It can save you heaps of trouble later on.

Cheers, glad you solved your problem!


Tux is my guildmaster.
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 3rd Jun 2008 14:24
...and when you start using templates...

you need to combine the defining with the "fleshing out" in one file - your header file.. Like C# kinda ... so that's not a terrible habit to get into.. even though I personally like the two file approach - "header" + "fleshy parts" = "full implementation"

but I digress.

Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 3rd Jun 2008 17:28
Quote: "Get in the habit of defining a function whenever you declare one. It can save you heaps of trouble later on. "


Actually, beyond the constructors, I tend to do the defining first. That way I get in the functions as I think about them and when I compile I get notification that it isn't a member of the class if I forgot to declare it and I can fix that.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Bishop
21
Years of Service
User Offline
Joined: 18th Dec 2002
Location: In my favorite chair...
Posted: 4th Jun 2008 08:23
I'm a single-file guy =P I prefer keeping all class declarations and definitions in headers. Get messy adding all sorts of files =P But that is, of course, my own opinion.

Quote: "Actually, beyond the constructors, I tend to do the defining first. That way I get in the functions as I think about them and when I compile I get notification that it isn't a member of the class if I forgot to declare it and I can fix that."


I intended to say, at the very least, even if you don't want to complete the functions, do this;



I personally think this is a good habit. Getting constructors/destructors in the proper order as well as at least defining the functions to a point where if they are call, it will not give a runtime error =P

My preffered way of handling this same class would be;



But again, just my method =P

Cheers


Tux is my guildmaster.

Login to post a reply

Server time is: 2024-09-29 21:18:01
Your offset time is: 2024-09-29 21:18:01