Thread: A vector of structs problem

  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

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Does your Spring need a copy constructor?
    C++ Copy constructors
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    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. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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