Thread: vector of object inside object not getting populated

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    77

    Question vector of object inside object not getting populated

    I have two class Particle and Spring.

    Spring:

    Code:
    classSpring{
    private:
        double ks;
        double kd;
        double l0;
        int orig, dest;
    
    public:
        Spring(int from, int to, double spring_const);
        glm::vec3 calculate_spring_force(glm::vec3 xa, glm::vec3 xb, double mass, glm::vec3 velocity);
    
    };
    Particle:

    Code:
    class Particle
    {
    private:
        glm::vec3 position;
        glm::vec3 velocity;
        glm::vec3 total_force;
        std::vector<Spring> springs;
        double mass;
        bool fixed;
    
    public:
        Particle(glm::vec3 pos);
        void add_spring(int from, int to, double spring_const);
        glm::vec3 get_position()
        {
            return position;
        }
        int get_spring_count()
        {
            springs.size();
        }
    };
    
    
    void Particle::add_spring(int from, int to, double spring_const)
    {
        springs.push_back(Spring(from, to, spring_const));
    }
    

    I need to have a vector of particles. And each particle can contain many springs.

    When I am populating the particles and springs inside main()
    Code:
    std::vector<Particle> particles;
    int particle_count = 0;
    for (size_t i = 0; i < n; i++)
    {
        for (size_t j = 0; j < n; j++)
        {
            Particle particle(particle_positions[particle_count]);
    
            for (size_t k = 0; k < conn.cols(); k++)
            {
                if (some_func(particle_count, k))
                {
                    particle.add_spring(particle_count, k, conn(particle_count, k));
                }
            }
            particles.push_back(particle);
            particle_count++;
        }
    }
    

    I expected to see many springs inside each particle but I see that each particle has 0 spring.I have checked every other functions and they are doing their job fine.

    Springs are not getting populated for some reason.
    I urge this respected community to look into this issue.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There is an obvious possibility: some_func is always returning false for the input you tested. Check with a debugger to see if some_func ever does return true, or temporarily change some_func to always return true.

    Another possibility is that conn.cols() always returns 0.

    A few suggestions unrelated to this problem:
    • get_position and get_spring_count should be declared as const member functions.
    • Instead of:
      Code:
      springs.push_back(Spring(from, to, spring_const));
      Use perfect forwarding with emplace_back:
      Code:
      springs.emplace_back(from, to, spring_const);
    • You can use particles.size() instead of your own particle_count variable.
    • The constructor for Particle should be declared explicit as you probably don't want:
      Code:
      Particle particle = particle_positions[particle_count];
    Last edited by laserlight; 06-14-2019 at 08:51 PM.
    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

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    77
    Thanks it got resolved. Thanks for the wonderful 'emplace_back' idea
    Last edited by progmateur; 06-15-2019 at 05:57 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Question On Inside the C++ Object Model
    By Antigloss in forum C++ Programming
    Replies: 1
    Last Post: 09-17-2010, 03:02 AM
  2. delete object from inside object
    By Drogin in forum C++ Programming
    Replies: 10
    Last Post: 01-02-2009, 08:55 PM
  3. Replies: 2
    Last Post: 11-28-2008, 05:15 PM
  4. Replies: 2
    Last Post: 04-02-2008, 06:30 AM
  5. Searching STL Map Inside STL Map Object :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2002, 09:11 AM

Tags for this Thread