Hi guys, i have been having problems with this piece of code all afternoon, i have never seen this error myself before and cant see how the problem is applicable in my situation from looking though the various forums of the world !

Basically the program is a small game where there are two cars that race each other to a pre defined point on the page, my problem lies in a small piece of code that updates the position of a car.

The error is detailed as: "Debugger exception notice: Project Drag.exe raised exception class EAccess violation. Access violation at adress 00401e13 in module drag.exe. Read of address 00000000".

I have read about null pointers but i dont not completely understand how they work with vectors, any help would be appreciated !

The vector is created in the TMainForm, using

private:	// User declarations

public:		// User declarations
std::vector<RacingCar*> cars ;

This section shows the stopwatch that is set to execute the race itself

void __fastcall TMainForm::StopWatchTimer(TObject *Sender)
{
    TTimer* timer = static_cast<TTimer*>(Sender);
    int dt = timer->Interval / 1000;

    {    // drive the cars
        for(int i = 0; i != cars.size(); ++i)
            cars[i]->Drive(dt);

        int i = 0;
        // update the race track
        Image2->Left += cars[i++]->DistanceTravelled();  /*This line is the one
        causing the error, when commented out rogram works fine.*/

    }

}

While there is no error shown in this section the code is related to it so i thoguht i should show it anyway...

void RacingCar::Drive(unsigned int time)
{
    //Determine when the winner has got tot he finish line ands make them stop
    if (mileometer <=1200)
    {
    // work our speed increase
    unsigned int speed_inc = Acceleration() * time;

    // check maximum speed is not exceeded
    if(speed + speed_inc > MaximumSpeed())
        speed_inc = MaximumSpeed() - speed;

    // calculate distance travelled
    mileometer += (speed + speed_inc) / 2 * time;

    Oil = random(100);
    if (Oil == 50) {
        speed = 0;
        speed_inc = 0;
        }

    // update speed
    speed += speed_inc;

    // apply drag
    if(Drag() > speed)
        speed = 0;
    else
        speed -= Drag();

    }
    else {
        speed = 0;
        winner = mileometer;
        }

}

Sorry for the info overload !

Sam

Could you post the code for the declaration of RacingCar?

RacingCar::RacingCar()
:mileometer(0)
,speed(0)
,Oil(0)
{
}
class RacingCar
  {
        public:
                RacingCar();
                void Drive(unsigned int time);
                unsigned int DistanceTravelled() const;
                unsigned int Winner() const;
        private:
                unsigned int Oil;
                unsigned int mileometer;
                unsigned int speed;
                unsigned int winner;
                virtual unsigned int Acceleration() const = 0;
                virtual unsigned int MaximumSpeed() const = 0;
                virtual unsigned int Drag() const = 0;
  } ;

Basically i have a copy of a program that is similar to mine that complies that was given to me by a lecturer but he is external so i havent seen him for 2-3 week so i cant get any help off him :/

Image2->Left += cars[i++]->DistanceTravelled();

Is I always supposed to be 1? Looking through the code you posted, it seems that I will always end up being 1. Is there a value in the vector cars at position 1?

One thing you can do to debug this would be to do the following:

cars[i++]->DistanceTravelled();

if you put this on a line by itself and it still crashes, the error occurs during accessing the vector. If it doesn't, then the error occurs when you try to assign a value to Image2->Left.

Yeah the error is in accesing the vector, i kind of assumed this was the problem from the start...

To answer the 1st question there is Image3->Left +=cars[i++]->DistanceTravelled() , the i++ basically acts to move on to the next car unless i have completely mis understood the way the program works from my lecturer.... xD

The Accelleration speed etc all come from a seperate unit (e.g. Ferrari.cpp) and since i have the program working in a different place/execution know the concept is fairly sound...

Sorry to bump, its only a small one though xD

Does anyone else have any ideas on this as i am still stumped royally and cant seem to find a way to make it work....

Could it be a problem with my debugger options ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.