0

header file I am using, teacher provided it so I doubt the error lies in here but it's throwing the unhandled exception

 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).

  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! I've been scraping my way across the internet for information why this is happening but I am not getting any reason why

  • 2
    For starters, the line `if(flightMap[i].adjacent_city=NULL)` was probably meant to read `if(flightMap[i].adjacent_city==NULL)` (note the change of `=` to `==`). That's most likely the cause of the immediate problem, actually. – Dietmar Kühl Oct 13 '13 at 22:27
  • This question appears to be off-topic because you are not doing anything incorrectly. – Kerrek SB Oct 13 '13 at 22:27
  • He said his program is behaving incorrectly, so it's on topic, however, it could be the case that problem is somewhere else, and not in the posted code. – Dialecticus Oct 13 '13 at 22:30
  • @Dialecticus You missed the biting sarcasm in Kerrek SB's comment. – us2012 Oct 13 '13 at 22:31

1 Answers1

0
flightMap[i].adjacent_city=NULL 

should be

flightMap[i].adjacent_city == nullptr // use nullptr instead of NULL or 0!
Raja
  • 2,846
  • 5
  • 19
  • 28