# Thread: A vector of structs problem

1. ## A vector of structs problem

Hello all,

Trying to work on some code - a struct with spring variables is loaded into a vector in an insert function, but the vector is empty in later functions. I suspect it has something to do with scopes or something, but am relatively new to std::vectors.

I think it is a relatively simple problem, so will just post the bits of code related to the vector (rather than a runnable program). Apologies for any confusion, but there is also another 'Vector' class used in my program - to represent 3 variables in a row.

Code:
```        struct Spring {
Vector equilPos; //represents equilibrium position of spring
float k;         //spring coefficient - represents tightness - high value makes them more rigid
float b;         //damping coefficient
Spring(){
k=0;
b=0;
equilPos.zero();
}
};

class PhysObj {
public:
//lots of unrelated functions / variables

std::vector<Spring> springs;
void add_spring(float springcoeff, float dampcoeff, float x, float y, float z);
//calculate forces (where the spring variables are actually used)
void forces(const State &state, float t, Vector &force, Vector &torque);
}

void PhysObj::add_spring(float springcoeff, float dampcoeff, float x, float y, float z){
Spring* tempspring = new Spring;
tempspring->equilPos.x = x;
tempspring->equilPos.y = y;
tempspring->equilPos.z = z;
tempspring->k = springcoeff;
tempspring->b = dampcoeff;
springs.push_back(*tempspring);
std::cout << "spring created, now "<< springs.size() << " spring, k="
<< springs[springs.size()-1].k << " b = "<< springs[springs.size()-1].b
<< std::endl;

}

void PhysObj::forces(const State &st, float t, Vector &force, Vector &torque) {
//springs
for (int i=0; i<springs.size(); i++){
Vector dampingforce = springs[i].b * st.vel;
force == (-springs[i].k) * st.pos - springs[i].equilPos)) - dampingforce;
}
std::cout << "Force: " << force.x << ",  " << force.y << ", " << force.z<< std::endl;
std::cout << "Number of springs: " << springs.size() << std::endl;
}```
The 'add_spring' function says there is 1 spring with the correct coefficients, the 'forces' function says there are 0 springs (and consequently, no forces).

Can someone help?

2. There is little I can say about the problem from this snippet. I would use a debugger and monitor when your springs count goes to 0.
Also, in your add_spring function, you realize that you are using new for absolutely no reason and have a memory leak, right?

3. Does your Spring need a copy constructor?
C++ Copy constructors

4. Grrr, my mistake.

It seems the code I posted was correct. It was the haphazard way I was testing it that was the problem. I was defining the 'PhysObj's in temp variables, then adding them to a Vector. When I got to adding the springs to the PhysObjs, I added the "add_spring" call at the end of the function - after they were in the vector. So the temporary object had a spring, but the one in the vector (which would later have its forces calculated) had no springs.

But thanks, I did eventually work out copy constructors and attempt one (no idea if it is any good):
Code:
```               Spring (const Spring &copyb) {
k=copyb.k;
b=copyb.b;
equilPos.x = copyb.equilPos.x;
equilPos.y = copyb.equilPos.y;
equilPos.z = copyb.equilPos.z;

}```

5. Actually, it looks like you do not need to define your own copy constructor for Spring as the compiler generated one will work if there is an appropriate copy constructor for Vector.