新手/学生程序员,试图找出这里出了什么问题。
我有一个“腿”类和一个“路线”类,其中路线是通过将较早的路线对象添加到腿对象来构建的。
路由类有两个构造函数;一种用于使用一条支路创建初始路线,另一种用于使用前一条路线和另一条支路构建后续路线。
一切似乎都很好
使用仅腿构造函数创建第一条路线
使用 leg+route 构造函数创建第二条路线
使用 leg+route 构造函数创建第三条路线
但是,如果我尝试以与创建第二个和第三个相同的方式创建第四个路由,则程序将编译,但当程序到达第一个路由对象时会崩溃。
代码如下:
/////////////////////////////
/* 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;
}
照原样,这编译并运行良好。
如果我删除周围的评论
/*
Route Route4( Route3, Leg4 );
cout << "Route built! Press [enter] to print" << endl << endl;
cin.get();
printRoute( cout, Route4);
*/
程序成功创建了腿,然后在到达第一条路线时崩溃。
我知道我必须这样做......指针有问题,但我不知道是什么以及为什么会导致程序以这种特殊方式崩溃。
如果有人比我更有洞察力,我将不胜感激。