Hi guys
I'm trying to finish my assignment, I stuck in a weird situation. I'm trying to create a pointer a linkedlist. This linkedlist contains pointer to a user-defined objects (City) and I'm trying to add City to this linkedlist and then iterate through the linked list to find and modify them.
Here is my City.h source code:
#include "List.h"
#include <iostream>
#include <string>
class City {
private:
std::string name;
List<City*> *nextCities;
bool visited;
public:
City(std::string);
void setNextCity(City c);
bool isVisited();
void setVisited();
std::string getName();
City* getNextUnvisitedCity();
};
City::City(std::string _name) {
name = _name;
visited = false;
nextCities = new List<City*>();
nextCities->push_back(this);
}
std::string City::getName() {
return this->name;
}
void City::setNextCity(City city) {
nextCities->push_back(&city);
};
bool City::isVisited() {
return visited;
}
void City::setVisited() {
visited = true;
}
City* City::getNextUnvisitedCity() {
List<City*>::iterator start = (*nextCities).begin();
List<City*>::iterator end = (*nextCities).end();
while(start != end) {
std::string ee = (*start)->getName();
*start++;
}
//temp
return NULL;
}
everything seems right when I try to add element into linkedlist:
void City::setNextCity(City city) {
nextCities->push_back(&city);
};
But when I try to iterate through them. I get memory violation error. I'm not sure that I defined the pointer correctly or not.
I'm using this version of linkedlist implementation with iterator:
http://fscked.org/writings/225notes/week4/c++std/list225.h