Hi,
I have three files - routeData.h, routeData.cpp, and main.cpp. Everything runs fine, but when I try to add another public int variable into routeData.h and set it's value in main.cpp to use in routeData.cpp, my program crashes as soon as it starts up - I can't figure out why! I seem to be doing the same thing with a different int variable that I declared in routeData.h and initiated in main.cpp. Here is my code:
routeData.h
Code:
#ifndef ROUTE
#define ROUTE
#include <set>
#include <list>
using namespace std;
typedef set<int> iSet;
typedef list<int> iLst;
typedef set<int>::iterator iSetPtr;
typedef list<int>::iterator iLstPtr;
class routeData
{
public:
int readRouteData(istream & inSource);
int distance(int first, int second);
int distance(iLst route);
iLst bestRoute(iLst done, iSet left);
int **myLocations;
int totalLocations;
int shortestDistance; // problem with this var
iLst finalRoute;
iLst holder;
iSet locs;
};
#endif
routeData.cpp
Code:
#include <iostream>
#include <set>
#include <list>
#include <cstdlib>
#include <string>
#include <sstream>
#include <ctype.h>
#include "routeData.h"
using namespace std;
iLst routeData::bestRoute(iLst done, iSet left)
{
int setSize;
iSetPtr setIterator;
iSetPtr tempPtr;
iLst finalList;
setSize = left.size();
if(setSize == 0)
{
cout << "PERMUTATION SEQUENCE FINISHED.\n";
cout << "->Distance of final permutation: ";
cout << distance(done) << "\n\n";
cout << "Comparison: distance of done = " << distance(done) << ", distance of shortestDistance = " << shortestDistance << "\n";
if((distance(done) < shortestDistance) || (shortestDistance == -1))
{
cout << "Distance of done is < shortestDistance\n";
shortestDistance = distance(done);
}
cout << "SHORTEST CURRENT DISTANCE: " << shortestDistance << "\n";
}
for(setIterator = left.begin(); setIterator != left.end(); setIterator++)
{
cout << "Iterator: " << *setIterator << "\n";
iLst tempList = done;
iSet tempSet = left;
tempSet.erase(*setIterator);
tempList.push_back(*setIterator);
iLstPtr tempLstPtr;
cout << "[ ";
for(tempLstPtr = tempList.begin(); tempLstPtr != tempList.end(); tempLstPtr++)
{
cout << *tempLstPtr << " ";
}
cout << "]\t";
iSetPtr tempPtr;
cout << "[ ";
for(tempPtr = tempSet.begin(); tempPtr != tempSet.end(); tempPtr++)
{
cout << *tempPtr << " ";
}
cout << "]\n";
cout << "Size of list: " << tempList.size() << "\n";
cout << "Size of set: " << tempSet.size() << "\n";
cout << "Total distance of this list: " << distance(tempList) << "\n\n";
bestRoute(tempList, tempSet);
}
return done;
}
main.cpp
Code:
#include <iostream>
#include <set>
#include <list>
#include <cstdlib>
#include "routeData.h"
using namespace std;
int main(int argc, char *argv[])
{
routeData myRoute;
int rrdResult;
myRoute.myLocations = new int*[myRoute.totalLocations];
for(int i = 0; i < 10; i++)
{
myRoute.myLocations[i] = new int[myRoute.totalLocations];
}
rrdResult = myRoute.readRouteData(cin);
if(rrdResult < 0)
{
cerr << "Bad input on line " << rrdResult;
exit(1);
}
for(int i = 1; i <= myRoute.totalLocations; i++)
{
if(i == 1)
{
myRoute.holder.push_back(i);
}
else
{
myRoute.locs.insert(i);
}
}
myRoute.shortestDistance = -1;
myRoute.finalRoute = myRoute.bestRoute(myRoute.holder, myRoute.locs);
cout << "----------------------------------\n";
cout << "DISTANCE OF LAST RETURNED ROUTE: ";
cout << myRoute.distance(myRoute.finalRoute) << "\n\n";
system("Pause");
return 0;
}
I have no idea why this makes my program crash. All it says is:
This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.
Any ideas?