run-time error(s)
hey there,
I was wondering if couild tell me what was wrong with the following pieces of code. it compiled error-free but during run-time it reported few errors. below is the source code:
/*Box.h*/
#ifndef BOX_H
#define BOX_H
class Box
{
public:
Box(double Length = 1.0, double Breadth = 1.0, double Height = 1.0);
double volume();
double getLength();
double getBreadth();
double getHeight();
int compareVolume(Box &otherBox) ;
private:
double length;
double breadth;
double height;
};
#endif
x--------x
/*List.h*/
#ifndef LIST_H
#define LIST_H
#include "Box.h"
class Package
{
public:
Package(Box *pNewBox);
Box *getBox();
Package *getNext();
void setNext(Package *pPackage);
private:
Box *pBox;
Package *pNext;
};
class TruckLoad
{
public:
TruckLoad();
TruckLoad(Box *pBox, int count);
Box *getFirstBox();
Box *getNextBox();
void addBox(Box *pBox);
private:
Package *pHead;
Package *pTail;
Package *pCurrent;
};
#endif
x--------x
/*Box.cpp*/
#include "Box.h"
Box::Box(double length, double breadth, double height)
{
length = length>0.0?length:1.0;
breadth = breadth>0.0?breadth:1.0;
height = height>0.0?height:1.0;
}
double Box::volume()
{
return length*breadth*height;
}
double Box::getLength()
{
return length;
}
double Box::getBreadth()
{
return breadth;
}
double Box::getHeight()
{
return height;
}
int Box::compareVolume(Box &otherBox)
{
double vol1 = volume();
double vol2 = otherBox.volume();
return vol1>vol2?1:(vol1<vol2?-1:0);
return 0;
}
/*CConApp95.cpp*/
x--------x
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
using namespace std;
#include "Box.h"
#include "List.h"
inline int random(int count)
{
return 1+static_cast<int>((count*static_cast<long>(rand() ))/(RAND_MAX+1));
}
int main()
{
const int dimLimit = 100;
TruckLoad load1;
for(int i=0;i<10;i++)
{
load1.addBox(new Box(random(dimLimit), random(dimLimit), random(dimLimit)));
}
Box *pBox = load1.getFirstBox();
Box *pNextBox;
while(pNextBox = load1.getNextBox())
{
if(pBox->compareVolume(*pNextBox)<0)
{
pBox = pNextBox;
}
}
cout<<endl
<<"The largest box in the first list is "
<<pBox->getLength()<<" by "
<<pBox->getBreadth()<<" by "
<<pBox->getHeight()<<endl;
const int boxCount = 20;
Box boxes[boxCount];
for(int i=0;i<boxCount;i++)
{
boxes[i] = Box(random(dimLimit), random (dimLimit), random(dimLimit));
}
TruckLoad load2(boxes, boxCount);
pBox = load2.getFirstBox();
while(pNextBox = load2.getNextBox())
{
if(pBox->compareVolume(*pNextBox)<0)
{
pBox = pNextBox;
}
}
cout<<endl
<<"The largest box in the second list is "
<<pBox->getLength()<<" by "
<<pBox->getBreadth()<< " by "
<<pBox->getHeight()<<endl;
pNextBox = load1.getFirstBox();
while(pNextBox)
{
delete pNextBox;
pNextBox = load1.getNextBox();
}
return 0;
}
/*List.cpp*/
x--------x
#include "Box.h"
#include "List.h"
Package::Package(Box *pNewBox)
{
}
Box *Package::getBox()
{
return pBox;
}
Package *Package::getNext()
{
return pNext;
}
void Package::setNext(Package *pPackage)
{
pNext = pPackage;
}
TruckLoad::TruckLoad(Box *pBox, int count)
{
pHead = pTail = pCurrent = 0;
if((count>0)&&(pBox!=0))
{
for(int i=0;i<count;i++)
{
addBox(pBox+i);
}
}
}
TruckLoad::TruckLoad()
{d
}
Box *TruckLoad::getFirstBox()
{
pCurrent = pHead;
return pCurrent->getBox();
}
Box *TruckLoad::getNextBox()
{
if(pCurrent)
pCurrent = pCurrent->getNext();
else
pCurrent = pHead;
return pCurrent?pCurrent->getBox():0;
}
void TruckLoad::addBox(Box *pBox)
{
Package *pPackage = new Package(pBox);
if(pHead)
pTail->setNext(pPackage);
else
pHead = pPackage;
pTail = pPackage;
}
|