I am executing code, and a c++ pointer is magically becoming null.
I have tried adding some print statements to my code to debug this.
Here is some code in my main class: ...
Vector3D* netForce = new Vector3D(0, forceY, 0);
Vector3D* accel = netForce->scalarMultiply(1.0/(*mass));
Vector3D *position = new Vector3D(75,initialHeight,0);
Vector3D* velocity = new Vector3D(0,0,0);
cout << "Sending in acceleration: " << *accel << "\n";
UberPhysics* uber = new UberPhysics(position, velocity, accel);
cout << "Uber acceleration: " << uber->getAcceleration();
...
Here is full source of the UberPhysics constructor:
UberPhysics::UberPhysics(Vector3D* position, Vector3D* velocity, Vector3D*
acceleration, Vector3D* jerk, Vector3D* hyperJerk) {
cout << "Check Acceleration: " << acceleration << endl;
this->position = position;
this->velocity = velocity;
this->acceleration = acceleration;
this->jerk = jerk;
this->hyperJerk = hyperJerk;
}
Here is the function implementation for scalarMultiply:
Vector3D* Vector3D::scalarMultiply(double c) {
Vector3D* v = new Vector3D(this->x*c, this->y*c, this->z*c);
return v;
}
Here is the operator<< override:
friend ostream& operator<<(ostream &os, Vector3D& v) {
os << "<" << v.getX() << "," << v.getY() << "," << v.getZ() << ">";
return os;
}
The "sending in acceleration: " print statement will print the vector. Therefore, it shows the vector is not null when I pass the acceleration to UberPhysics. When the constructor runs, it is saying that the acceleration pointer is 0. I could see this being a problem if I had passed acceleration as a pointer to a local variable on the stack, but the Vector3D functions use the new operator to allocate the class on the heap. Does anyone know what the problem could be?