A vector of structs problem

This is a discussion on A vector of structs problem within the C++ Programming forums, part of the General Programming Boards category; Hello all, Trying to work on some code - a struct with spring variables is loaded into a vector in ...

  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 05:18 AM. Reason: folding

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    21,781
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 10: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, 05:24 PM
  5. register structs - problem declaring
    By Kagey in forum C Programming
    Replies: 11
    Last Post: 11-11-2002, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21