c++ - std::vector not retaining data? -
i have code:
void baseobj::update(baseobj* surround[3][3]) { forces[0]->apply(); //in place of loop cout << forces[0]->getstrength() << endl; //forces std::vector of force* } void baseobj::addforce(float str, int newdir, int lifet, float lifelength) {
force newforce; newforce.init(draw, str, newdir, lifet, lifelength); forces.insert(forces.end(), &newforce); cout << forces[0]->getstrength();
}
now, when call addforce , make infinite force strength of one, cout's 1. when update called, outputs 0, if force no longer there.
you storing pointer force in vector force function local.
you must use new
create in on heap.
force* f = new force; forces.push_back(f);
Comments
Post a Comment