views:

39

answers:

4

I have two classes, Car and Person. Car has as one of its members an instance of Person, driver. I want to move a car, while keeping track of its location, and also move the driver inside the car and get its location. However, while this works from inside the class (I have printed out the values as they are calculated), when I try to access the data from main, there's nothing there. I.e. the array position[] ends up empty. I am wondering if there is something wrong with the way I have set up the classes -- could it be a problem of the scope of the object?

I have tried simplifying the code so that I only give what is necessary. Hopefully that covers everything that you would need to see. The constructer Car() fills the offset array of driver with nonzero values.

class Car{

public: 

       Container(float=0,float=0,float=0);
       ~Container();
       void move(float);
       void getPosition(float[]);
       void getDriverPosition(float[]);

private:

       float position[3];
       Person driver;
       float heading;
       float velocity;

};


class Person{

public: 

       Person(float=0,float=0,float=0);
       ~Person();
       void setOffset(float=0,float=0,float=0);
       void setPosition(float=0,float=0,float=0);
       void getOffset(float[]);
       void getPosition(float[]);

private:

       float position[3];
       float offset[3];
};

Some of the functions:

void Car::move(float time){

    float distance = velocity*time;
    location[0] += distance*cos(PI/2 - heading);
    location[1] += distance*sin(PI/2 - heading);

    float driverLocation [3];
    float offset[3];
    driver.getOffset(offset);

    for (int i = 0; i < 3; i++){
       driverLocation[i] = offset[i] + location[i];
    }
    driver.setPosition(driverLocation[0],driverLocation[1],driverLocation[2]);
}

void Car::getDriverPosition(float p[]){

    driver.getPosition(p);
}

void Person::getPosition(float p[]){

    for (int i = 0; i < 3; i++){
       p[i] = position[i];
    }
}

void Person::getOffset(float o[]){

    for (int i = 0; i < 3; i++){
       o[i] = offset[i];
    }
}

In Main:

Car * car = new Car();
car->move();
float p[3];
car->getDriverPosition(p);

When I print driverLocation[] inside the move() function, I have actual nonzero values. When I print p[] inside main, all I get are zeros.

Newly found problem: Through some debugging, I've discovered every time that I call move() the destructor for Person class is called, and thus deletes all the data. Anyone have an idea about how this could happen? I don't call the destructor anywhere, how do I prevent it from happening.

A: 

You never passed the driverLocation array into person -- how is it supposed to know it got moved? (i.e. you need to call driver->setPosition(driverLocation) inside move)

Billy ONeal
Thank you. I have updated the question, as new information has changed what could be wrong.
Hypatia
A: 

in the code you show you never seem to set person.position

you set a local variable in move called driverLocation.

pm100
yes, thank you. it seems I left that out of my code written here, but I had that line in code, still doesn't work. I have changed my question, as I've narrowed down what the real problem is.
Hypatia
we have to trust you that driver.setposition is doing the right thing - since yu don show it
pm100
A: 

The problem with the code above is the line

driver->getOffset(offset);

The private member driver is not a pointer type. Change this to: driver.getOffset(offset)

Another solution would be something like this:

// header
class Car
{
  private:
    Person* driver;

  public:
    Car();
    ~Car();
}

// source
Car::Car()
{
  driver = new Person();
}

Car::~Car()
{
  delete driver;
}
Holger Kretzschmar
Thank you. you are right. I think I just messed that up copying it over. Still doesn't solve the problem that the destructor keeps getting called after every call to move()
Hypatia
A: 

So I think that I figured it out. I think the problem was that I was passing the object by value into the move() function. I switched to passing it as a pointer to the object, and it worked!

Hypatia