File: bodies.cpp
for (int i=0; i<n; ++i) {
phys_vector pos{xdist(re), ydist(re)};
double mass = mdist(re);
body b{pos.x, pos.y, mass};
bodies.push_back(b);
}
File: bodies.h
public:
bodies_aos() = default;
private:
std::vector<phys_vector> compute_forces(const simulation_parameters & param);
private:
std::vector<body> bodies;
};
My intention is to define all the variables outside the loop. My approach (which afterwards I found out it was incorrect, as it does not return the same results) is the following one:
bodies.cpp ->Modified
int i;
double mass;
vector<phys_vector> pos;
std::vector<body> b;
for (i=0; i<n; ++i) {
phys_vector pos{xdist(re), ydist(re)};
mass = mdist(re);
body b{pos.x, pos.y, mass};
bodies.push_back(b);
}
Unfortunately, it did not return the same results due to a bad initialization of variables b
and/or pos
, but it does not raise any errors when compiling.
Does anyone knows how this could be solved in order to obtain the same results as in the first case?