Novice / Student programmer, trying to figure out what's broken here.
I have a 'leg' class and a 'route' class, where the route is built by adding an earlier route object to a leg object.
The route class has two constructors; one to create the initial route using one leg, and one for building subsequent routes using the previous route plus another leg.
Everything seems to work fine for
creating the first route with the leg-only constructor
creating the second route with the leg+route constructor
creating the third route with the leg+route constructor
however, if i try to create a fourth route in the same manner I created the second and third, the program will compile, but will crash when the program reaches the first route object.
code as follows:
/////////////////////////////
/* includes and namespaces */
/////////////////////////////
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <ostream>
using std::ostream;
#include <cstdlib> // use for: atof(), atoi()
///////////////////////
/* const definitions */
///////////////////////
////////////////////////
/* struct definitions */
////////////////////////
///////////////////////
/* class definitions */
///////////////////////
class Route;
class Leg
{
private:
const char* const startCity;
const char* const endCity;
const int length;
public:
Leg( const char* const, const char* const, const int );
friend void printLeg( ostream&, const Leg&);
friend void printRoute( ostream&, const Route& );
};
class Route
{
private:
const Leg** legsPtrArray;
const int totalLegs;
public:
Route( const Leg& );
Route( const Route&, const Leg& );
Route( const Route&);
~Route();
friend void printRoute( ostream&, const Route& );
};
/////////////////////////
/* function prototypes */
/////////////////////////
///////////////////
/* main function */
///////////////////
int main()
{
Leg Leg1( "San Francisco", "Reno", 218 );
Leg Leg2( "Reno", "Salt Lake City", 518 );
Leg Leg3( "Salt Lake City", "Kansas City", 604 );
Leg Leg4( "Kansas City", "Indianapolis", 482 );
Leg Leg5( "indianapolis", "NYC", 709 );
cout << "Legs loaded, press [enter] to print each leg" << endl;
cin.get();
cout << "Leg 1: " << endl << endl;
printLeg( cout, Leg1 );
cin.get();
cout << "Leg 2: " << endl << endl;
printLeg( cout, Leg2 );
cin.get();
cout << "Leg 3: " << endl << endl;
printLeg( cout, Leg3 );
cin.get();
cout << "Leg 4: " << endl << endl;
printLeg( cout, Leg4 );
cin.get();
cout << "Leg 5: " << endl << endl;
printLeg( cout, Leg5 );
cin.get();
cout << "Building complete route: " << endl << endl;
Route Route1( Leg1 );
Route Route2( Route1, Leg2 );
Route Route3( Route2, Leg3 );
cout << "Route built! Press [enter] to print" << endl << endl;
cin.get();
printRoute( cout, Route3);
/*
Route Route4( Route3, Leg4 );
cout << "Route built! Press [enter] to print" << endl << endl;
cin.get();
printRoute( cout, Route4);
*/
/*
Route Route5( Route4, Leg5 );
cout << "Route built! Press [enter] to print" << endl << endl;
cin.get();
printRoute( cout, Route5);
*/
cout << endl;
cout << "Press [enter] to quit " << endl;
cin.get();
}
//////////////////////////
/* function definitions */
//////////////////////////
Leg::Leg( const char* const startCityToLoad, const char* const endCityToLoad, const int lengthToLoad )
: startCity( startCityToLoad ), endCity( endCityToLoad ), length( lengthToLoad )
{
}
void printLeg( ostream& out, const Leg& legToPrint )
{
out << "Leg start city: " << legToPrint.startCity << endl;
out << "Leg end city: " << legToPrint.endCity << endl;
out << "Leg length: " << legToPrint.length << " miles" << endl;
}
Route::Route( const Leg& legToAdd)
: totalLegs( 1 ), legsPtrArray ( new const Leg*[totalLegs] )
{
legsPtrArray[0] = &legToAdd;
}
Route::Route( const Route& subRoute, const Leg& legToAdd)
: totalLegs( subRoute.totalLegs + 1 ), legsPtrArray ( new const Leg*[totalLegs] )
{
for (int i = 0; i < subRoute.totalLegs ; i++)
{
legsPtrArray[i] = subRoute.legsPtrArray[i];
}
legsPtrArray[subRoute.totalLegs] = &legToAdd;
}
Route::Route( const Route& routeCopy )
: totalLegs( routeCopy.totalLegs ), legsPtrArray ( routeCopy.legsPtrArray )
{
}
Route::~Route()
{
delete[] legsPtrArray;
}
void printRoute( ostream& out, const Route& routeToPrint )
{
out << "Route: from "
<< routeToPrint.legsPtrArray[0]->startCity
<< " to "
<< routeToPrint.legsPtrArray[0]->endCity
;
for (int i = 1; i < routeToPrint.totalLegs; i++)
{
out
<< " to "
<< routeToPrint.legsPtrArray[i]->endCity;
}
out << endl << endl;
int routeLength = 0;
for (int i = 0; i < routeToPrint.totalLegs; i++)
{
routeLength =
routeLength + routeToPrint.legsPtrArray[i]->length;
}
out << "Route Length: " << routeLength << " miles" << endl;
}
As-is, this compiles and runs fine.
If I remove the comments around
/*
Route Route4( Route3, Leg4 );
cout << "Route built! Press [enter] to print" << endl << endl;
cin.get();
printRoute( cout, Route4);
*/
The program successfully creates the legs, then crashes when it reaches the first route.
I know I must be doing.... something wrong with the pointers, but I can't figure out what and why that would be causing the program to crash in this particular way.
If anyone has more insight than me I'd appreciate the help.