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