c++ - Access violation for a specific function -


i have many functions in customerlist.cpp file, 1 of doesn't work shown below (and break point marked comment). note: store class correct, , m_phead customerlist private variable (but shouldn't matter).

bool customerlist::removestore(int id) {      store *back, *temp;      if(m_phead = null)         {             cout << "\nerror! store " << id << " not found in list!\n";             system("pause");             return false; // nothing delete         }      // search item delete     = null;     temp = m_phead;      while((temp != null) && (temp->getstoreid() != id))     {         = temp;         temp = temp->m_pnext;     }      if(back == null)    // delete first item in list     {         m_phead = temp->m_pnext; // function breaks here         delete temp;         cout << "\nsuccess! store " << id << " added list!\n";         system("pause");         return true;     }     else if(temp != null)  // delete middle or end of list     {         back->m_pnext = temp->m_pnext;         delete temp;         cout << "\nsuccess! store " << id << " added list!\n";         system("pause");         return true;     }     else     {         cout << "\nerror! store " << id << " not found in list!\n";         system("pause");         return false;    // didn't find item delete     }  } 

every time make call function, breaks, if id of store not on list (it shouldn't make far in function). here example of call:

// creating new customer list customerlist *newlist = new customerlist(); newlist->removestore(3); 

what in world doing wrong?

there logic errors in code. notably, line;

if(m_phead = null) 

is assigning null m_phead before comparing it. thus, temp null, , back remains null, why code reaches spot commented , crashes.

you need use == comparison operator, not = assignment operator (your compiler should have warned that):

if(m_phead == null) 

or safer:

if(!m_phead) 

now, said, can simplify rest of code following:

bool customerlist::removestore(int id) {     store *temp, *previous;      // search item delete      previous = null;     temp = m_phead;      while (temp != null)     {         if (temp->getstoreid() == id)         {             if (m_phead == temp)             {                 // deleting first item in list                 m_phead = temp->m_pnext;             }              if (previous != null)             {                 // deleting middle or end of list                 previous->m_pnext = temp->m_pnext;             }              delete temp;             cout << "\nsuccess! store " << id << " removed list!\n";             system("pause");             return true;         }          previous = temp;         temp = temp->m_pnext;     }      cout << "\nerror! store " << id << " not found in list!\n";     system("pause");     return false; // nothing delete } 

or, if use standard c++ container, such std::list, instead of making own manual linked-list, can instead:

struct isstoreid {     int m_id;     isstoreid(int id) : m_id(id) {}     bool operator()(const store &store) { return (store.getstoreid() == m_id); } };  bool customerlist::removestore(int id) {     // search item delete      // m_list std::list<store>...     std::list<store>::iterator iter = std::find_if(m_list.begin(), m_list.end(), isstoreid(id));      bool bwasfound = (iter != m_list.end());     if (bwasfound)     {         m_list.erase(iter);         cout << "\nsuccess! store " << id << " removed list!\n";     }     else         cout << "\nerror! store " << id << " not found in list!\n";      system("pause");     return bwasfound; } 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -