Converting an iterator to a pointer to delete objects

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?
Last edited on
Post relevant code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;
}
My most recent attempt was using
1
2
3
4
	for ( iter = LS.begin(); iter != LS.end(); iter++ ) {
		p_rec = &*iter;
		delete p_rec;
	}


This got the program to run, but then when the program gets to the line
delete p_rec;

I get a window popping up that says
Debug Assertion Failed!
Program: ...blahblah.exe
File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgdel.cpp
Line: 52

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)


I don't understand this message, but apparently I'm still doing something wrong with delete.
Never mind, I just discovered the list.erase() function
Topic archived. No new replies allowed.