Problem with list<class>

Hi peoples.
I'm making a program that enters a person requests a list using list <class>.
My code creates a person object without error, creates the requested object, but when I try to print the list of applications it gives error.

The code for printing is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void Pessoa::ImprimirPedidos() {
    if (pedido.empty())
        cout << "Empty List\n";
    else {
        Pedido pedAux;
        list<Pedido> aux;
        aux.clear();
        aux.swap(pedido);

        for (int i = 0; i < aux.size(); i++) {
            cout << "\nSize of list " << aux.size();
            cout << "\nSize os iterator \n" << i;
            pedAux = aux.front();
            cout << "\nOK 1 ";
            aux.erase(aux.begin());
            cout << "\n OK 2 ";
            pedido.insert(pedido.end(), 1, (const Pedido) pedAux);
            cout << "\n OK 3 ";
            cout << "Request: \n";
            pedAux.Display();
        }
    }
}


The error is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Stack trace:
Frame     Function  Args
0028C744  74AB1194  (00000114, 0000EA60, 00000000, 0028C868)
0028C758  74AB1148  (00000114, 0000EA60, 000000A4, 0028C84C)
0028C868  610C92FA  (00000000, 00000114, 0028C888, 00000000)
0028C948  610C6057  (00000000, 00000000, 00000000, 00000000)
0028C998  610C643C  (00000F9C, 0028C9C0, 00000011, 611663A0)
0028CA58  610C6551  (00000F9C, 00000006, 0028CA88, 610C65F5)
0028CA68  610C658C  (00000006, 0028CE80, 0028CA98, 611289CB)
0028CA88  610C65F5  (00000000, 611663A0, 0028CAB8, 610EA511)
Exception: STATUS_ACCESS_VIOLATION at eip=61026867
eax=610F52DD ebx=6116A300 ecx=00000030 edx=00000034 esi=00000008 edi=18D1CC80
ebp=18D1C8E8 esp=18D1C8E0 program=C:\Users\mutiley\Documents\NetBeansProjects\CarrinhoDeCompras\dist\Debug\Cygwin_1-Windows\carrinhodecompras.exe, pid 3996, thread sig
cs=0023 ds=002B es=002B fs=0053 gs=002B ss=002B
Stack trace:
Frame     Function  Args
18D1C8E8  61026867  (6116A300, 611AA725, 610EA511, 00000000)
18D1C908  6102783C  (0028C72C, 00000001, 00000001, 00000000)
18D1C938  61028375  (00000058, 18D1C974, 00000000, 00000000)
18D1CC58  6102899C  (000000C4, 18D1CC80, 000000A4, 18D1CD28)
18D1CD38  610C9DEB  (61169220, 00000000, 00000000, 00000000)
18D1CD68  61003F51  (00000000, 00000000, 00000000, 61004A63)
End of stack trace



What am I doing wrong in the loop?


PS: the complete code is here
http://www.4shared.com/file/5iRCQ7JT/CarrinhoDeCompras.html
thanks
Last edited on
I don't see what's causing that error, but you are WAY overcomplicating this function.

You are creating a separate list, swapping with another list, then removing each element and inserting it back into the original list. This is all pointless.

If the goal is to just display each item in the list, then just iterate over the list normally:

1
2
3
4
5
6
7
8
9
10
void Pessoa::ImprimirPedidos() {
    if (pedido.empty())
        cout << "Empty List\n";
    else {
        for(list<Pedido>::iterator i = pedido.begin(); i != pedido.end(); ++i)
        {
            i->Display();
        }
    }
}


Much simpler, right?


But like I said, even though your code is roundabout and overly complicated, I don't see anything in it that would cause a crash. Do you know which line the crash is occuring on?
"Disch", this way you say is easier .I did not know how to treat the iterator . With this code the program compiled, printed each element of the list but is still giving the error "Stack Trace:" I said in first message.
thanks for the help.
The error is not in the code you posted. It must be somewhere else in the program.

Exactly what line does the crash occur on?
I don't know exactly the line of code where the problem occurs Disch.
The compiler simply runs the program and the results are shown along with one stack at the end. The gdb don't displays where the error occur. I use the IDE Netbeans7.0.1 for the Windows 7.
thanks for yours help.
Try to compile with debug flags -ggdb
Also, if you are going to use messages to debug use cerr instead of cout.
It flushes the output (and you can separate streams)

Edit: I cannot reproduce the crash.
Last edited on
hi guys.
Thanks for all help. The problem is when I insert the second object of request class. The function I did have created:
1
2
3
void Pessoa::InserePedido(Pedido novo){
       pedido.insert(pedido.begin(), novo);
}

The function are wrong sure. She insert aways in the same position of memory. I go create a for loop for increase the position of memory and so I post the result here for speak if ran or not.
Topic archived. No new replies allowed.