Thread: A vector of structs problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    25

    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?
    Last edited by Salem; 01-04-2011 at 06:18 AM. Reason: folding

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Problem with structs and functions
    By Moose112 in forum C++ Programming
    Replies: 11
    Last Post: 12-01-2006, 11:04 PM
  3. Problem with array of structs
    By Wiretron in forum C Programming
    Replies: 3
    Last Post: 09-03-2006, 02:07 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. register structs - problem declaring
    By Kagey in forum C Programming
    Replies: 11
    Last Post: 11-11-2002, 03:58 AM