For some reason the function doesn't save member values for an array of objects except the first one. I condensed the code to highlight the nature of this problem.
classFile.cpp
myClass objectArray[10];
void myClass::Set(float x, int elementID)
{
myX = x;
log<< myX; //output is equal to x
log<< elementID; //output ranges from 0-9
log<< objectArray[elementID].myX; //output is incorrect if elementID is higher than 0
}
classFile.h
extern myClass objectArray[10];
callingFunctionFile.cpp
for(int i=0; i<10;i++)
{
objectArray[i].Set(5.0f, i);
i++;
}
The incorrect output of objectArray[elementID].myX
is always the same for specific elementID
but differs between other elementID
s. Sometimes it's 0, sometimes it's something like 8231924021
Edit: Here's the original code with relevant parts written at the top (in case if you notice some nuances cause it's just too long to actually go through it): classFile.h, classFile.cpp, functionCallingFile.cpp
Edit2:
It looks like the position of the class members is shifted in the memory 3 bytes per each object and that makes it impossible to read them except the first one. Here's the comparision of the myX
addresses and objectArray[i].myX
addresses - pastebinLink
I could just counter this 3 bytes shift manually but it's like spraying deodorant into the toiled instead of flushing it.
Edit3:
What solved the problem was changing the position of #include "classFile.h"
inside callingFunctionFile.cpp file. After placing it at the top as the first include the problem disappeared
You are incrementing
i
twice in the loop, making you skip every other element in the array.