More dynamic array truble

This is a discussion on More dynamic array truble within the C++ Programming forums, part of the General Programming Boards category; I have a class that I have problems with accesing some of the information from Code: class Scene { public: ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    More dynamic array truble

    I have a class that I have problems with accesing some of the information from
    Code:
    class Scene
    {
         public:
          ...
          Sphere getSphere(int i)
    
          private:
          Sphere** sphereList;
    }
    Here is where I add info to the sphereList
    Code:
    void Scene::createScene()
     {
     	 //Create a container for all the spheres
     	 sphereList = new Sphere*[100];
     	 
     	 // Create the scene
     	 // ------------------------------------------
     	 
     		 // Sphere1 - a blue one
     		 // A few variables for the first sphere
     		 Vector3D pos1(50,0,0);
     		 Material blue(0,0,200);
     		 // Create the first sphere
     		 sphereList[0] = new Sphere;
     		 sphereList[0]->setPosition(pos1);
     		 sphereList[0]->setRadius(50);
     		 sphereList[0]->setMaterial(blue);
     
     
     		 // Sphere2 - a red one
     		 // A few variables for the second sphere
     		 Vector3D pos2(-50,0,0);
     		 Material red(200,0,0);
     		 // Create the second sphere
     		 sphereList[1] = new Sphere;
     		 sphereList[1]->setPosition(pos2);
     		 sphereList[1]->setRadius(50);
     		 sphereList[1]->setMaterial(red);
     		 
     		  // Sphere3 - a green one
     		 // A few variables for the second sphere
     		 Vector3D pos3(0,-50,0);
     		 Material green(200,0,0);
     		 // Create the second sphere
     		 sphereList[2] = new Sphere;
     		 sphereList[2]->setPosition(pos2);
     		 sphereList[2]->setRadius(50);
     		 sphereList[2]->setMaterial(green);
     		 
     		 /*
     		 // Sphere3 - a light source
     		 sphereList[2] = new Sphere;
     		 sphereList[2]->setLight(1);
     		 */
     	 
     	 // ------------------------------------------	
     	 // Done with the scene
     	 
     	 // Set how many spheres there are, to make stuff
     	 // easier
     	 noOfObjects = 2;
     	 
     	 // Print some info
     	 std::cout << "Scene sucsesfully created" << std::endl;
     	 std::cout << "Two spheres created! " << std::endl;
     }
    And this is how I acces the list later on:
    Code:
    Sphere Scene::getSphere(int i)
    {
         return *sphereList[i];
    }
    But for some reason I dont get the right sphere returned. I looks like getSphere(0) returns the first sphere, but all other values return the last spere.

    Any ideas on what is wrong ?

    Thanks

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by h3ro View Post
    Code:
     		  // Sphere3 - a green one
     		 // A few variables for the second sphere
     		 Vector3D pos3(0,-50,0);
     		 Material green(200,0,0);
     		 // Create the second sphere
     		 sphereList[2] = new Sphere;
     		 sphereList[2]->setPosition(pos2);
     		 sphereList[2]->setRadius(50);
     		 sphereList[2]->setMaterial(green);
     		 
     		 /*
     		 // Sphere3 - a light source
     		 sphereList[2] = new Sphere;
     		 sphereList[2]->setLight(1);
     		 */
    You reuse index 2. Oops?

    Code:
    Sphere Scene::getSphere(int i)
    {
         return *sphereList[i];
    }
    This returns a Sphere by value. This means you need to either declare an appropriate assignment operator and copy constructor, or the class needs to be designed such that the default ones the compiler provides work properly.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    You reuse index 2. Oops?
    No, I just commented out the light, as I have not added that function so far. I guess I could delete it, but I kind of likes to be reminded of that I have to add it later on

    This returns a Sphere by value. This means you need to either declare an appropriate assignment operator and copy constructor, or the class needs to be designed such that the default ones the compiler provides work properly.
    So I need to do this?
    Code:
    Sphere Scene::getSphere(int i)
    {
         Sphere returnSphere = sphereList[i];
         return returnSphere 
    }
    Sorry for asking for the spoon feeding here, but pointers and dynamic arrays are a mistery so far for me.

    EDIT:
    I think its allready is returning a sphere, as I do a printing of the position of the sphere later on in my code, and then I get the position of one of the spheres. Or am I wrong?

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    This looks like a bug
    Code:
       sphereList[1]->setPosition(pos2);
       // ...
       sphereList[2]->setPosition(pos2);
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    This is definitely a disgusting example, name wise, but it shows that you really don't have to declare another variable:

    Code:
    class thing
    {
    public:
        int thingy;
    };
    
    class thing2
    {
    public:
        thing thingy2[4];
    
        thing &getat( int pos )
        {
            return thingy2[pos];
        }
    };
    
    int main( void )
    {
        thing2 t2;
        thing  t;
    
        t2.thingy2[2].thingy = 42;
    
        t = t2.getat(2);
    
        std::cout<< t.thingy;
    
        return 0;
    }
    in the getSphere() function. Returning a local variable is definitely unnecessary!

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > Material green(200,0,0);
    Maybe:
    Code:
     		 Material green(0,200,0);
    > sphereList[2]->setPosition(pos2);
    Maybe:
    Code:
     		 sphereList[2]->setPosition(pos3);

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > Sphere** sphereList;
    I'm not sure why spereList needs to be a double pointer, but you may have a good reason. Why not just:
    Code:
          Sphere* sphereList;
    .
    .
    void Scene::createScene()
     {
     	 //Create a container for all the spheres
     	 sphereList = new Sphere[100];
    .
    .
     		 sphereList[0].setPosition(pos1);
     		 sphereList[0].setRadius(50);
     		 sphereList[0].setMaterial(blue);

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    I found out that this part of my program accually works, the problem was that I had "sphereList[2]->setPosition(pos2);" which made it look like something was wrong.

    This is part of a raytracer I am writing, and for some reason the raytracer will only trace one object. So no matter how many spheres I have in my scene, only the last one will be displayed. Driving me mad!

    Thanks for your time and effort

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginner: dynamic array of strings
    By pc2-brazil in forum C++ Programming
    Replies: 10
    Last Post: 04-29-2008, 04:29 PM
  2. need help with dynamic array syntax
    By soldyne in forum C Programming
    Replies: 3
    Last Post: 10-11-2005, 01:59 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 09:02 PM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 11:48 AM

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