Hi everyone, I'm writing a program to practice using iterators and lists from STL. The program continually prompts the user to input data about an employee (name, age, etc.) then creates a new object of class Emp_rec to contain that information. As the objects are created, I place them into list<Emp_rec> in a certain order. I navigate through the list to find the object's correct place by using an iterator I call "iter". Once the user has inputted all the employees they want, I reprint the list of employees in order. At the end, I'm pretty sure I need to delete the objects to prevent a memory leak, but I'm not sure how to do that. Using "delete iter;" gives me an error...
error C2440: '=' : cannot convert from 'std::_List_iterator<_Mylist>' to 'Emp_rec *'
I get what the error message is saying, but I'm not sure how to convert the list iterator into a pointer that will let me delete the objects at the end of the program. Any ideas on how to delete these objects at the end of the program?
list<Emp_rec> LS;
list<Emp_rec>::iterator iter;
Emp_rec* p_rec;
p_rec = new Emp_rec( last, first, age, job_level );
for ( iter = LS.begin(); iter != LS.end() && *p_rec > *iter; iter++ );
LS.insert( iter, *p_rec );
//code to print the list
for ( iter = LS.begin(); iter != LS.end(); iter++ ) {
delete iter;
}
There's a condensed version of the code. And the error with this code being run is...
error C2440: 'delete' : cannot convert from 'std::_List_iterator<_Mylist>' to 'void *'
The error I had printed earlier resulted from trying to use
1 2 3 4
for ( iter = LS.begin(); iter != LS.end(); iter++ ) {
p_rec = iter;
delete p_rec;
}