I can see a small mistake.
73 74 75 76 77 78 79
|
//galaxyobjectstotal=totalobjects;
galaxyobjectsptr = new galaxyobject[ galaxyobjectstotal];
// set to null
cout << "\r\nSet up initial values to galaxy objects as null...";
for(int i=0; i<= galaxyobjectstotal; i++)
|
I quoted more code than the mistake itself (which is
underlined) in order to provide context.
As you know, in C and C++ counting starts from 0, not 1.
This means that an array
A that has 3 elements will provide
A[0],
A[1] and
A[2].
It is important to notice that
A[3] does not exist.
Trying to access
A[3],
A[4] or
A[627] means that you're going out of bounds. This is undefined behavior, which in turn means that any of these things can happen randomly:
a) it works and you get 0
b) it works and you get a garbage value (e.g. -68476)
c) it crashes with a segmentation fault
So you are allocating an array of length
galaxyobjectstotal.
This means that the the index
i of the
for() loop can go as high as
(galaxyobjectstotal - 1) and no higher.
1 2 3
|
for(int i=0; i < galaxyobjectstotal; i++) // same as
for(int i=0; i <= galaxyobjectstotal - 1; i++) // but the above is preferred by most people
|
Anyway, I am writing code that creates a dynamic array. I would like to be able to do something like
galaxyobject[object] -> uniqueid = in the class but I do not think I have it setup right. |
From what I see, you already figured out that the correct form is:
galaxyobject[object].uniqueid
Also don't forget to put a
delete[] galaxyobjectsptr;
somewhere, perhaps in the destructor.