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);

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

04-18-2007

brewbuck

Quote:

Originally Posted by h3ro

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);

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.

04-18-2007

h3ro

Quote:

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 :p

Quote:

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.

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!