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 / Initialising global arrays

Author
Message
scd
13
Years of Service
User Offline
Joined: 5th Nov 2010
Location: New Zealand
Posted: 7th Mar 2011 12:03
This should be a fairly simple question but I cannot find an adequate answer on Google. It is best demonstrated with some simple code:

This compiles fine:



But this produces a compile error about trying to redefine the size of the array:



But when put inside a function the above code no longer produces an error:



What is going on here? I ask because I want to define a large global array of objects and initializing them as in the first example is not possible.

Cheers
Sam
Cetobasilius
14
Years of Service
User Offline
Joined: 29th Dec 2009
Location: Mexico
Posted: 7th Mar 2011 13:00 Edited at: 7th Mar 2011 13:16
this is the correct way to define a global array

or


when you declare numbers[3] there will be 3 elements, element 0, element 1 and element 2, so when trying to access the first slot, you do: numbers[0]=something; for slot number 3 it would be: numbers[2]=something;

Quote: "But this produces a compile error about trying to redefine the size of the array:"
you are not redefining the size of the array there, you are simply assigning a value 3 times to the same slot. to redefine an arrays size you must delete it and make it again, there are other classes that can have this caracteristic though.

the size is defined by the number between []

if you do not specify what this array contains, and is a global variable, the compiler will fill it with zeros.

for example, if you were to make a global array of 10 ints it would go like this:


suppose that arrays are like a lot of variables with the same name, only with a different number.

all of these variables for example


could be contained in an array of ints like so:

and to access this array, you would use

this, can only be done inside a method because there would be no sense on declaring a variable as any number and adding another number before it is even used. and besides, nothing outside a method or function or class can be executed besides the declaration of variables

hi
scd
13
Years of Service
User Offline
Joined: 5th Nov 2010
Location: New Zealand
Posted: 8th Mar 2011 00:19
Oh man my apologies that made me sound like a complete idiot! I didn't mean to have the same index for each one in the example. Sorry about that but thanks for the reply. Those were just code snippets I wrote to illustrate the problem. I'll try and describe the problem I'm actually having...

I have a Waypoint class with a member array that contains pointers to adjacent waypoints. Which waypoints are connected obviously has to be hard-coded by me. Currently I am trying to do it like this:



But now I know that nothing outside a method or function is executed except for declarations (thanks ) I need a better way to do this. Any ideas?
Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 11th Mar 2011 21:01
If the array is a member of the class, why don't you initialize it in the constructor of the class?

If it's a global array, then write a function to initialize the array and then call the function once at the start of the program.
scd
13
Years of Service
User Offline
Joined: 5th Nov 2010
Location: New Zealand
Posted: 13th Mar 2011 23:16
Yeah I ended up defining a 2D array to express the relationships between the waypoints then used a member function to take the data relevant to each waypoint. Now my enemy can navigate my map!

Thanks for all the help
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 18th Mar 2011 09:02
It can also be done to any class.

All this is in global space.... Or you can put it into a function. It works either way. Keep in mind this is an initialization of the array.

The fastest code is the code never written.

Login to post a reply

Server time is: 2024-06-27 22:05:48
Your offset time is: 2024-06-27 22:05:48