0

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

4
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by alwaysLearning0
0
City* City::getNextUnvisitedCity() {
    
    // This is not working here!!
    City *last = nextCities->GetLastElement();
    
    // TODO: return a proper object
    return NULL;

}

As you can see you have left a TODO: , should I have to complete that TODO: ?

so you just have to iterate all the cities and find the last un visited city and
return it's pointer.

TODO: means you left the code to future write.

0
City* City::getNextUnvisitedCity() {
    
    // This is not working here!!
    City *last = nextCities->GetLastElement();
    
    // TODO: return a proper object
    return NULL;

}

As you can see you have left a TODO: , should I have to complete that TODO: ?

so you just have to iterate all the cities and find the last un visited city and
return it's pointer.

TODO: means you left the code to future write.

Thanks mate for your quick replay
That TODO is for myself. The main problem is I cannot access element in nextCities. As I mentioned in the code that for example: *last return bad pointer and don't know why!

0

Hi,

you are adding local vairble to the list, so when you are accessing it, that memory is already freed as it was local vairble.

void City::setNextCity(City city) {    

//city is local vaible here!!
nextCities->AddElement(&city);
    
// This is working fine! IT WILL WORK AS CITY IS STILL VALID REFERENCE    
City* test = nextCities->GetLastElement();};

I havent post the fix, i guess you will be able to figure out how to fix it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.