I am writing a .cpp and .h file for the EmployeeDatabase class, which has a func
ID: 3917785 • Letter: I
Question
I am writing a .cpp and .h file for the EmployeeDatabase class, which has a function addEmployee(EmployeeRecord *e) where e is a pointer to an existing object of EmployeeRecord type. This function takes e and inserts that object into a binary tree. Here is my function, but I am getting an error: Access violation reading location 0xCDCDCDCD. when it cycles through the while loop for the second time. Is there anything blatantly obvious in this function that would cause this? Please answer ASAP
bool EmployeeDatabase::addEmployee(EmployeeRecord *e){
cout << "3";
EmployeeRecord *temp, *back;
temp = m_pRoot;
back = NULL;
while(temp!=NULL){
back = temp;
if(e->getID() < temp->getID())
temp = temp->m_pLeft;
else
temp = temp->m_pRight;
}
if(back==NULL)
m_pRoot = e;
else{
if(e->getID() < back->getID())
back->m_pLeft = e;
else
back->m_pRight = e;
}
return true;
}
Explanation / Answer
Looks like you are trying to access an object which has not been initialized properly. Please make sure whichever object e is pointing to which is passed to this function and is initialized and allocated memory. Also m_pRoot has been allocated memory. Your function definition has all the pointer either pointing to e or m_pRoot, if either of this is pointing to null or has not initialized properly then above error is encountered.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.