Hi guys
I'm struggling to write a code for traversing a directed graph. It contains City as nodes. I'm trying to put City(s) to a LinkedList and then work with them to find the path. However, as you will notice I'm still newbie in C++. I have a *nextcities as property in City class. There is a function to add related Cities to a node. As you can see in setNextCity(City c) in works fine! and I can check my Cities in the list BUT in getNextUnvisitedCity(), I cannot access *nextcities and it returns BadPtr!! Can you drop some hints for me?
#include "LinkedList.h"
class City {
private:
string name;
cLinkedList<City> *nextCities;
bool visited;
public:
City(string);
void setNextCity(City c);
bool isVisited();
void setVisited();
string getName();
City* getNextUnvisitedCity();
};
City::City(string _name) {
name = _name;
visited = false;
nextCities = new cLinkedList<City>();
}
string City::getName() {
return name;
}
void City::setNextCity(City city) {
nextCities->AddElement(&city);
// This is working fine!
City* test = nextCities->GetLastElement();
};
bool City::isVisited() {
return visited;
}
void City::setVisited() {
visited = true;
}
City* City::getNextUnvisitedCity() {
// This is not working here!!
City *last = nextCities->GetLastElement();
// TODO: return a proper object
return NULL;
}
Cheers