Program error?
Jul 29, 2015 at 2:49am Jul 29, 2015 at 2:49am UTC
This code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
#include<iostream>
#include<string>
#include<vector>
using namespace std;
//Car class
class Car
{
private :
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
public :
//Default constructor
Car()
{
}
//Parameterized constructor
Car(string _reportingMark,int _carNumber,string _kind,bool _loaded,string _destination)
{
reportingMark = _reportingMark;
carNumber = _carNumber;
kind = _kind;
loaded = _loaded;
destination = _destination;
}
//Destructor
~Car()
{
}
//Copy constructor
Car( const Car &other)
{
reportingMark = other.reportingMark;
carNumber = other.carNumber;
kind = other.kind;
loaded = other.loaded;
destination = other.destination;
}
Car operator =(Car other)
{
reportingMark = other.reportingMark;
carNumber = other.carNumber;
kind = other.kind;
loaded = other.loaded;
destination = other.destination;
return *this ;
}
friend bool operator ==(Car c1,Car c2);
void setUp()
{
cout<<"Implementing Progress\n" ;
}
void output()
{
cout<<"Reporting Mark: " << reportingMark<<endl;
cout<<"Card Number: " << carNumber<<endl;
cout<<"Kind: " << kind<<endl;
cout<<"Loaded: " << loaded<<endl;
cout<<"Destination: " << destination<<endl;
}
};
//Friend function
bool operator ==(Car c1,Car c2)
{
if (c1.reportingMark==c2.reportingMark
&& c1.carNumber == c2.carNumber
&& c1.kind == c2.kind
&& c1.loaded == c2.loaded
&& c1.destination == c2.destination)
{
return true ;
}
else
{
return false ;
}
}
/* ********** StringOfCars member functions ********** */
class StringOfCar
{
private :
Car *cars; //pointer of cars
int carCount; //defined additional data member carCount
public :
//Default constructor
StringOfCar()
{
cars=NULL; //set initially NULL and 0
carCount=0;
}
//Parameterized constructor
StringOfCar(Car *_cars)
{
cars = _cars;
carCount=0;
}
//Copy construtor
StringOfCar( const StringOfCar &obj)
{
cars = obj.cars;
carCount=0;
}
//Destructor
~StringOfCar()
{
//deleting each car allocated
for (int i=0;i<carCount;i++)
delete cars;
}
int getCount() //UFD here defined here
{return carCount;}
void output()
{
for (int i=0; i<carCount; i++)
{
cars[i].output();
}
}
void push(Car car)
{
cars[carCount++]=car;
}
Car pop()
{
Car lastObj = cars[--carCount]; //getting top of stack of cars
return lastObj;
}
void input()
{
cout<<"In progress!\n" ;
}
};
int main()
{
/* ********Create a Car object named car1 with the following constants as initial values:****/
Car car1("SP" ,34567 ,"business" ,true ,"Salt Lake City " );
cout<<"---------- TEST1 car1 object content--------------\n" ;
car1.output();
/* ******** Create a Car object named car2 using the default constructor. */
Car car2;
//Use the = operator to copy the data from car1 to car 2.
car2 = car1;
cout<<"\n----------car2 object content--------------\n" ;
//Print car2
car2.output();
//Create a default StringOfCars object named string1.
StringOfCar string1;
string1.push(car1);
//Print: STRING 1
cout<<"\n---------- TEST2 string1 object content--------------\n" ;
string1.output();
//Create a car named car3.
Car car3;
car3 = string1.pop();
//Print car3.
cout<<"\n----------TEST3 car3 object content--------------\n" ;
car3.output();
//Then print the contents of string1 again
cout<<"\n----------string1 object content--------------\n" ;
string1.output();
return 0;
}
When I try to run it in codeblocks it pauses as if the application had an error, I think it has something to do with push but I'm not exactly sure. Any ideas?
Jul 29, 2015 at 9:19am Jul 29, 2015 at 9:19am UTC
Your program crashes on line 175 -> 141 -> cars
is NULL. Nonetheless you are trying to assign a car. You need to do more in the push(...) function. Better use a std::vector.
Topic archived. No new replies allowed.