Thursday, 15 May 2014

c++ - Access violation for a specific function -


I have many functions in my CustomerList.cpp file, not just one thing (and a comment to the break point Has been marked with). Note: Store class is correct, and M_PHD is a customerlist private variable (but this should not make any difference).

  bool CustomerList :: removeStore (int id) {store * back, * temp; If (m_pHead = NULL) {cout & lt; & Lt; "Error \ Store!" & Lt; & Lt; Id & lt; & Lt; "Not found in the list! \ N"; System ("pause"); return false; // do not delete anything} // Search for item for retraction = faucet; Temp = m_pHead; While ((temp! = Null) and amp; (temp-> getStoreID ()! = Id)) {back = floating; Temp = temp- & gt; M_p Next; } If (back == NULL) // Remove the first item in the list {m_pHead = temp-> m_pNext; // function bad temp here; Cout & lt; & Lt; "\ N Success! Store" & lt; & Lt; Id & lt; & Lt; "Added to the list! \ N"; System ("pause"); Back true; } And if (temp! = NULL) // Delete from the middle or end of the list {back-> M_pNext = temp-> M_pNext; Remove Temp; Cout & lt; & Lt; "\ N Success! Store" & lt; & Lt; Id & lt; & Lt; "Added to the list! \ N"; System ("pause"); Back true; } And {cout & lt; & Lt; "Error \ Store!" & Lt; & Lt; Id & lt; & Lt; "Not found in the list! \ N"; System ("pause"); return false; // did not get to delete item}}  

Every time I call this function, it breaks, even if the store id is not in the list (it It should not be removed at the ceremony). Here is an example of a call:

  // Preparing a new customer list customer list * newList = new CustomerList (); NewList-> RemoveStore (3);  

What am I doing wrong in the world?

There are some logic errors in your code, most notably this line;

  If (m_pHead = NULL)  

what assign NULL to M_pHead Compare this before. Thus, the temporary is zero, and the back remains in the tap, which is why your code reaches the comment and the location you crash.

You need to use it == comparison operator, not = assignment operator (your compiler should warn you about it):

  if (m_pHead == NULL)  

or secure:

  if (! M_pHead)  

Now, that said, you can do the rest of the code in the following:

  bool CustomerList :: removeStore (int id) {store * temporary, * last; // Previous = Search items to remove Nouvelle; Temp = m_pHead; While (temp! = NULL) {if (temp-> getStoreID () == ID) {if (m_pHead == temp) {// delete the first item in the list m_pHead = temp-> m_pNext; } If (Previous! = Null) {// Delete from the middle or end of the list Previous-> M_pNext = temp-> M_p Next; } Temporary removal; Cout & lt; & Lt; "\ N Success! Store" & lt; & Lt; Id & lt; & Lt; "Deleted from the list! \ N"; System ("pause"); Back true; } Previous = Temporary; Temp = temp- & gt; M_p Next; } Cout & lt; & Lt; "Error \ Store!" & Lt; & Lt; Id & lt; & Lt; "Not found in the list! \ N"; System ("pause"); return false; // nothing delete}  

Or, if you use a standard C + container, such as std :: list , your manual link- Instead of creating a list, you can do it instead:

  struct isStoreID {int m_id; IsStoreID (int id): m_id (id) {} ​​bool operator () (const store & store) {returns (store.getStoreID () == m_id); }}; Bool CustomerList :: removeStore (int id) {// search for deleting items // Where m_list is a std :: list & lt; Store & gt; ... std :: list & lt; Store & gt; :: 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 & lt; & Lt; "\ N Success! Store" & lt; & Lt; Id & lt; & Lt; "Deleted from the list! \ N"; } And cout & lt; & Lt; "Error \ Store!" & Lt; & Lt; Id & lt; & Lt; "Not found in the list! \ N"; System ("pause"); Return bWasFound; }  

No comments:

Post a Comment