Thread: Constructor problem in list of classes

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    71

    Constructor problem in list of classes

    Hey all,

    I have a list of classes enclosed in a class:


    Code:
    /* The controling class of the whole program. Calls the functions responsible for displacement calculation and object rendering,
     * as well as containing the list of ClStar objects
     */
    
    #ifndef CLASS_UNIVERSE_H
        #define CLASS_UNIVERSE_H
    
    #include <list>
    #include <iostream>
    #include "ClassStar.h"
    #include "ClassValues.h"
    #include "ClassMath.h"
    #include "ClassGravCalculator.h"
    #include "ClassRender.h"
    #include "ClassPosRegulator.h"
    
    using namespace std;
    
    
    class ClUniverse {
        private:
    
        list<ClStar> m_Stars;
    
        ClPosRegulator m_ClPosReg;
        ClGravCalculator m_ClGravCalc;
    
        public:
    
        ClUniverse();
    
        void calculateDisplacement();
        void drawObjects();
    };
    
    #endif
    Code:
    #include "ClassUniverse.h"
    
    
    ClUniverse::ClUniverse() : m_Stars(ClValues::nNumObjects)
    {}
    
    void ClUniverse::calculateDisplacement()
    {
        list<ClStar>::iterator it_NxtStar;
        for(list<ClStar>::iterator it_CurStar = m_Stars.begin(); it_CurStar != m_Stars.end(); it_CurStar++) {
            it_NxtStar = it_CurStar;
            it_NxtStar++;
            while(it_NxtStar != m_Stars.end()) {
                m_ClGravCalc(*it_CurStar, *it_NxtStar);
                cout << it_CurStar->getPosition().getX() << endl;
                cout << it_CurStar->getPosition().getY() << endl;
                cout << it_NxtStar->getPosition().getX() << endl;
                cout << it_NxtStar->getPosition().getY() << endl;
                cout << endl;
                it_NxtStar++;
            }
            m_ClPosReg(*it_CurStar);
        }
    }
    
    void ClUniverse::drawObjects()
    {
        clear(ClValues::dblbuffer);
    
        drawing_mode(DRAW_MODE_TRANS, 0, 0, 0);
    
        for(list<ClStar>::iterator it_CurStar = m_Stars.begin(); it_CurStar != m_Stars.end(); it_CurStar++)
            ClRender(ClValues::dblbuffer, it_CurStar->getPosition(), it_CurStar->getRadius());
    
        vsync();
        blit(ClValues::dblbuffer, screen, 0, 0, 0, 0, ClValues::nScreenHight, ClValues::nScreenWidth);
    }
    Here are the ClStar files:

    Code:
    /* This file contains the declarations for ClStar. It contains variables to hold all the necessary data to simulate gravitation displacement
     * between stars using classical mechanics
     */
    
    #ifndef CLASS_STARS_H
        #define CLASS_STARS_H
    
    #include <cmath>
    #include "ClassMath.h"
    #include "ClassVector.h"
    #include "ClassValues.h"
    #include "ClassRange.h"
    
    class ClStar {
        private:
    
        //Mass
        long double m_dMass;
    
        //Density
        long double m_dDensity;
    
        //Radius
        long double m_dRadius;
    
        //Position vector
        ClVector m_vecPosition;
    
        //Velocity vector
        ClVector m_vecVelocity;
    
        //The number of stars
        static int s_nNumStars;
    
        //---------------------------------------------
    
        /*//Next node in linked list
        ClStar* m_nextnode;*/
    
        //---------------------------------------------
    
        public:
    
        //Constructor, will use vales from the class ClValues
        //ClStar(int nNumNodes);
        ClStar();
    
        //Access functions
        long double getMass();
        long double getDensity();
        long double getRadius();
        ClVector getPosition();
        ClVector getVelocity();
    
        void setPosition(ClVector vecPosition);
        void setPosition(long double dX, long double dY);
        void addPosition(ClVector vecPosition);
        void addPosition(long double dX, long double dY);
    
        void setVelocity(ClVector vecVelocity);
        void setVelocity(long double dX, long double dY);
        void addVelocity(ClVector vecVelocity);
        void addVelocity(long double dX, long double dY);
    
        //ClStar* getNextNode();
    
    };
    
    #endif
    Code:
    #include "ClassStar.h"
    
    int ClStar::s_nNumStars = 0;
    
    /*ClStar::ClStar(int nNumNodes) : m_nextnode(0)
    {
        //Generate random values for mass and density, using ranges found in ClValues,
        //and compute the radius. This program treats the objects as spheres.
        m_dMass = ClMath::randNum(ClValues::rnMass);
        m_dDensity = ClMath::randNum(ClValues::rnDensity);
        m_dRadius = std::pow((m_dMass/m_dDensity) / (4 * ClMath::dPI / 3), static_cast<long double>(1.0/3.0));
    
        //Get the sequence number of current node. Starts at 1
        int nCurNode = ClValues::nNumObjects - nNumNodes + 1;
    
        //The screen will be divided into "quadrants" to lower the chances of two stars sharing coordinates. One quadrant will be as
        //high as the screen, and as wide as ScrWidth/NumObjects
        int nQuadrantWidth = static_cast<int>(floor(ClValues::nScreenWidth / ClValues::nNumObjects));
        ClRange<long int> rngWidth(nQuadrantWidth * (nCurNode - 1), nQuadrantWidth * nCurNode - 1);
        ClRange<long int> rngHight(0, ClValues::nScreenHight);
    
        //Generate coordinates
        m_vecPosition.setVector(static_cast<long double> (ClMath::randNum(rngHight)), static_cast<long double> (ClMath::randNum(rngWidth)));
    
        //Create new node if needed ammount has not yet been reached
        if(nNumNodes - 1)
            m_nextnode = new ClStar(nNumNodes - 1);
    
    }*/
    
    ClStar::ClStar()
    {
        //Generate random values for mass and density, using ranges found in ClValues,
        //and compute the radius. This program treats the objects as spheres.
        m_dMass = ClMath::randNum(ClValues::rnMass);
        m_dDensity = ClMath::randNum(ClValues::rnDensity);
        m_dRadius = std::pow((m_dMass/m_dDensity) / (4 * ClMath::dPI / 3), static_cast<long double>(1.0/3.0));
    
        //Number of the star
        int nCurNode = ++s_nNumStars;
    
        //The screen will be divided into "quadrants" to lower the chances of two stars sharing coordinates. One quadrant will be as
        //high as the screen, and as wide as ScrWidth/NumObjects
        long int nQuadrantWidth = static_cast<long int>(floor(ClValues::nScreenWidth / ClValues::nNumObjects));
        ClRange<long int> rngWidth(nQuadrantWidth * (nCurNode - 1), nQuadrantWidth * nCurNode - 1);
        ClRange<long int> rngHight(0, ClValues::nScreenHight);
    
        //Generate coordinates
        m_vecPosition.setVector(static_cast<long double> (ClMath::randNum(rngHight)), static_cast<long double> (ClMath::randNum(rngWidth)));
    }
    
    long double ClStar::getMass()
    {
        return m_dMass;
    }
    
    long double ClStar::getDensity()
    {
        return m_dDensity;
    }
    
    long double ClStar::getRadius()
    {
        return m_dRadius;
    }
    
    ClVector ClStar::getPosition()
    {
        return m_vecPosition;
    }
    
    ClVector ClStar::getVelocity()
    {
        return m_vecVelocity;
    }
    
    void ClStar::setPosition(ClVector vecPosition)
    {
        m_vecPosition = vecPosition;
    }
    
    void ClStar::setPosition(long double dX, long double dY)
    {
        ClVector vec(dX, dY);
        m_vecPosition = vec;
    }
    
    void ClStar::addPosition(ClVector vecPosition)
    {
        m_vecPosition += vecPosition;
    }
    
    void ClStar::addPosition(long double dX, long double dY)
    {
        ClVector vec(dX, dY);
        m_vecPosition += vec;
    }
    
    void ClStar::setVelocity(ClVector vecVelocity)
    {
        m_vecVelocity = vecVelocity;
    }
    
    void ClStar::setVelocity(long double dX, long double dY)
    {
        ClVector vec(dX, dY);
        m_vecVelocity = vec;
    }
    
    void ClStar::addVelocity(ClVector vecVelocity)
    {
        m_vecVelocity += vecVelocity;
    }
    
    void ClStar::addVelocity(long double dX, long double dY)
    {
        ClVector vec(dX, dY);
        m_vecVelocity += vec;
    }
    Problem is, all the "stars" get generated with exactly the same coordinates. Before, when I made my own linked list, the code worked fine. But when I rewrote the code to use lists instead, it doesn't work anymore. Any tips as to why this is happening?

    Cheers,

    Gabe

    PS.: Ignore anything that's commented out, it's just the old code using linked lists. And sorry for the debug stuff that's strewn all over the place.
    Last edited by G4B3; 05-29-2009 at 09:31 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM