Hi
In main() I have a line of code
unitList.push_back(addUnit())
where addUnit is my unit creation function which returns an object of type Unit, and unitList is a vector for Units (type defined as ListOfUnits). This appears to work as intended in all circumstances
Elsewhere, I have a function from main which is passed unitList by reference:
The function's prototype
Army createArmy(ListOfUnits &unitList)
which gives it access to the vector, since the vector is not a global, but exists within main.
There is also a vector of my Army class. This class holds a pointer to the Unit type that the army consists of. In the createArmy function, it is sometimes necessary to add a new unit type, and since I have passed in the units vector by reference, I have tried to accomplish this with
unitList.push_back(addUnit())
But weird behaviour ensues!!
If I skip this step and make a number of armies with existing units, everything works properly. However, if I make a new army and want to make a new unit to go into that army, the push-back(addUnit()) line is called. This corrupts one part of the FIRST element in the Army vector, regardless of size. The corruption is that the pointer to a Unit type in the Army class is corrupted, and when access is attempted, this crashes the program. The debugger flags this pointer, which as I said is weirdly in the first element of the vector,as a bad pointer.
Slow and painful debugging has proven that the pointer is fine before unitList.push_back(addUnit()) is called from within the createUnit function, but is corrupt immediately after. I have absolutely no idea what could be causing this.
Does anyone out there have a clue?