Why is this throwing an unhandled exception?
this is the header file I am using and the function that is throwing errors, was providing by professor so it has to be right
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
template<class ItemType>
class Node
{
private:
ItemType item;
Node<ItemType> *next;
public:
Node(); //default constructor
Node(const ItemType &anItem); //none default constructor #1
Node(const ItemType &anItem, Node<ItemType> *nextPtr); //none default constructor #2
void setItem(const ItemType &anItem);
void setNext(Node<ItemType> *nextPtr);
ItemType getItem() const;
Node<ItemType> *getNext() const;
};
/*other functions*/
template<class ItemType>
Node<ItemType>* Node<ItemType>::getNext() const {
return next;
}
|
The function that I am getting in trouble with is this one (I tested all prior functions which worked fine).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
bool getNextUnvisitedCity(char citeh, char &adjCity){
if(!(isInMap(citeh)))
return false;
else {
for(int i=0; i<9; i++){
if(flightMap[i].city_name==citeh){
if(flightMap[i].adjacent_city=NULL)
return false;
Node<char>* tempPtr;
tempPtr=flightMap[i].adjacent_city;
while(tempPtr->getNext()!=NULL){
if(!(isVisited(tempPtr->getItem()))){
adjCity=tempPtr->getItem();
return true;
}
else
tempPtr=tempPtr->getNext();
}
if(flightMap[i].adjacent_city->getNext()!=NULL){
if(!(isVisited(flightMap[i].adjacent_city->getItem()))) {
adjCity=flightMap[i].adjacent_city->getItem();
return true;
} else
return false;
} } } } }
|
the error I'm getting is
Unhandled exception at 0x00fe4a76 in Assignment 12.exe: 0xC0000005: Access violation reading location 0x00000004.
and the specific error is
|
this | 0x00000000 {item=??? next=??? } | const Node<char> * const
|
I failed my test because of the SAME exact thing but I'm not doing anything incorrectly!
If adjacent_city on line 11 can be nullptr, then lines 13, 22, 23 and 24 will result in dereferencing nullptr.
Topic archived. No new replies allowed.