This my new code which should fix the dequeue problem but I am still getting that error message. Could you guys look at the void readInput() function but because i think it may have something to do with that.
Code:
//Brian Justice
//Assignment 6
// 5/17/12
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
using namespace std;
class Queue;
class City;
class Cell {
friend class Queue;
Cell *next; City *object;
public:
Cell (City *o, Cell *n) { next = n; object = o; }
};
class City {
friend class Queue;
friend class DataOrganizer;
Queue *q; // List of cities this one is directly connected to
City *from; // To trace a path back to origin
string name; // Name of the city
bool mark; // false=unmarked, true=visiting or done
public:
City ();
void setName (string theName);
void neighbor (City *c);
City *next ();
void setMark();
void setFrom(City* g);
void displayBestRoute ();
bool checkMark();
//~City ();
};
//################## Queue class ######################
class Queue {
friend class City;
Cell *tail;
public:
Queue() { tail = NULL; }
void enqueue(City *t) {
if (t == NULL) return;
if (tail == NULL) {
tail = new Cell(t, NULL);
tail->next = tail;
}
else {
Cell *h = new Cell (t, tail->next);
tail->next = h;
tail = h;
}
}
City *dequeue() {
if (tail == NULL) return NULL;
Cell *ptr = tail->next;
City *t = ptr->object;
if (ptr != tail) tail->next = ptr->next;
else tail = NULL;
delete ptr;
return t;
}
int isEmpty() { return tail == NULL; }
};
City:: City () { q = new Queue(); from = NULL; mark = false; name = ""; }
void City::setName (string theName) { name = theName; }
void City::neighbor (City *c) { q->enqueue(c); }
City *City::next () { return (City *)q->dequeue(); }
void City::setMark() { mark = true; }
void City::setFrom(City* g) { from = g; }
bool City::checkMark() {
if (mark == true) return true;
if (mark == false) return false;
}
void City::displayBestRoute () { // displays route back to city of origin
cout << name << " ";
while (from != NULL) {
cout << from->name << " ";
from = from;
}
}
//City::~City() { delete q; if (name != NULL) delete name; }
///// Organizes Data
class DataOrganizer {
int count; // Counts the number of cities
string name; // accepts input from the file
City **cities;
public:
DataOrganizer () { count = 0; }
void readInput (char *filename) {
fstream *fin = new fstream(filename, ios::in);
while (true) {
string buffer;
*fin >> buffer;
if (buffer == "-") break;
count ++; }
fin->close();
fin->clear();
string tok;
cities = new City*[count];
fin->open(filename, ios::in);
for (int i=0; *fin>>tok && tok != "-"; i++) cities[i]->setName(tok);
for (int i=0; i < count; i++) {
for (int j=0; *fin>>tok && tok != "-"; j++) {
int n;
istringstream buffer(tok);
buffer >> n;
cities[i]->neighbor(cities[n]);
}
}
fin->close();
fin->clear();
}
City *getOrigin() { return cities[0]; }
City *getDestination() { return cities[count]; }
};
int main (int argc, char **argv) {
if (argc != 2) {
cout << "Usage: " << argv[0] << " \n";
exit(0);
}
DataOrganizer dao;
dao.readInput(argv[1]);
// Set origin and destination cities
City *origin = dao.getOrigin();
City *destin = dao.getDestination();
City *city;
Queue *q = new Queue (); // Simulate simultaneity
origin->setMark();
q->enqueue(origin);
while (!q->isEmpty()) {
City *current = (City*) q->dequeue();
while((city = current->next()) != NULL) {
if (city == destin) {
city->setFrom(current);
cout << "Origin: " << origin << endl;
cout << "Destin: " << destin << endl;
city->displayBestRoute();
return 0;
}
if (city->checkMark() != true) {
city->setMark();
city->setFrom(current);
q->enqueue(city);
}
}
}
cout << "No Route Between Selected Cities\n";
}