lundi 16 mars 2015

Access violation for a specific function


I have many functions in my CustomerList.cpp file, the only one of which that doesn't work is shown below (and the break point is marked with a comment). NOTE: Store class is correct, and m_pHead is a CustomerList private variable (but that shouldn't matter).



bool CustomerList::removeStore(int ID)
{

Store *back, *temp;

if(m_pHead = NULL)
{
cout << "\nError! Store " << ID << " not found in the List!\n";
system("pause");
return false; // nothing to delete
}

// Search for the item to delete
back = NULL;
temp = m_pHead;

while((temp != NULL) && (temp->getStoreID() != ID))
{
back = temp;
temp = temp->m_pNext;
}

if(back == NULL) // Delete the first item in the list
{
m_pHead = temp->m_pNext; // THE FUNCTION BREAKS HERE
delete temp;
cout << "\nSuccess! Store " << ID << " added to List!\n";
system("pause");
return true;
}
else if(temp != NULL) // Delete from middle or end of list
{
back->m_pNext = temp->m_pNext;
delete temp;
cout << "\nSuccess! Store " << ID << " added to List!\n";
system("pause");
return true;
}
else
{
cout << "\nError! Store " << ID << " not found in the List!\n";
system("pause");
return false; // Didn't find the item to delete
}

}


Every time I make a call to this function, it breaks, even if the ID of the Store is not on the list (it shouldn't make it that far in the function). Here is an example of a call:



// Creating a new Customer List
CustomerList *newList = new CustomerList();
newList->removeStore(3);


What in the world am I doing wrong?




Aucun commentaire:

Enregistrer un commentaire