queue

so i made this queue

any my teacher wants me to be able to run it though this:
1
2
3
 void show(const Queue &waitingLine)
{
     }


so i made this:
1
2
3
4
5
6
7
8
9
10
11
12
 void show(const Queue &waitingLine)
{
     Queue temp;
     temp = waitingLine;
     
     int j = temp.count();
     for(int i = 0; i < j; i++)
     {
             cout << temp.pop().getName() << endl;
             } 
     return;
     }


but every time i run it it causes a segmentation error when i try to pop something from my queue


it's hard to say without seeing your code. Maybe you're deleting the front and returning a reference to it with pop.
i dont think its the Queue its self ... seams to work all this time... i think its the fact that this methods taking in a const Queue

but heres the rest of the code for Queue

ok i have tested it a few times and like 1 out of 2 times it seamed to work idky
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "Queue.h"
#include "bankCustomer.cpp"
#include "bankCustomer.h"
#include <string>
#include <iostream>

using namespace std;

Queue::Queue()
{
              front = NULL;
              back = NULL;
              }

void Queue::push_back(bankCustomer d)
{
     node *temp;
     temp = new node;
     temp->data = d;
     temp->next = NULL;
     
     if(front == NULL)
     {
              front = back = temp;
              }
     
     back->next = temp;
     back = back->next;
     return;
     }
     
bankCustomer Queue::pop()
{
         node *temp;
             bankCustomer b;    
             
             if(front == NULL)
             {
                      cout << "no nodes" << endl;
                      }
             if(front == back && front != NULL)
             {
                      temp = front;
                      b = front->data;
                      front = NULL;
                      back = NULL;
                      delete temp;
                      return b;
                      }
             
             b = front->data;
             temp = front;
             front = front->next;
             delete temp;
             return b;
             }
             
int Queue::count()
{
     node *temp;
     int x = 0;
     
     if(front == NULL)
     {
              cout << "Nothing" << endl;
              return 0;
              }
              else if(front == back)
              {
                   return 1;
               }
              else
              {
                  for(temp = front; temp != NULL;temp = temp->next)
                           x++;
                  }
              return x;
              
     }


Last edited on
if you didn't overload the =, it's only copying member per member, not making a copy of all your nodes. Maybe that's it, though I would have expected it to just pop successfully but leave the passed queue with hanging pointers.
i would expect it to work as well xD
i tryed copying the nodes but it thows errors when i try and pop the const queue
try adding this:
1
2
3
4
5
6
7
8
9
10
11
12
13
Queue& Queue::operator=(const Queue& right) {
	if (this != &right) { // check for self assignment
		while (front) { // clear this object
			node *temp = front;
			front = front->next;
			delete temp;
		}
		back = NULL;
		for (node *p = right.front; p; p = p->next) // push right into this object
			push_back(p->data);
	}
	return *this;
}
wow xD i an currently learning this xD
see i tryed doing it inside the function but this awsome xD now i see the point in using operator overloading lol
Topic archived. No new replies allowed.