Assignament operator for a queue not working properly

I've been playing around with dynamic allocation and queue recently. Keep in mind that I am fairly new to data structures and I’ve been teaching myself by watching YouTube tutorials and reading some tutorials from websites so forgive my ignorance if I don’t know every single thing there is to know about this subject. However, while playing around with the implementation of an assignment operator for my queue, I seem to have made a mistake somewhere in its code. You see whenever I try and use the assignment operator

Queue names2;
names2 = names;

the assignment operator copies everything already in the queue except the empty elements of the array. Am I implementing my copy constructor or my assignment operator wrong that is causing it to not add the empty unused capacity to the newly created queue?
Last edited on

Queue.h
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
#ifndef Queue_H
#define Queue_H

#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>

using namespace std;

class Queue {
public:
    Queue(); //Defualt costructor
    Queue(const Queue& source); //Copy constructor
    ~Queue(); //Destructor

    Queue& operator=(const Queue& source); //Assignament Operator //NOT WORKING

    void enqueue(string value); // add item to queue
    string dequeue(); // remove item from queue

    void showFullQueue() const;

    //memory allocation
    void memory(int currentCapacity);
private:
    void shift();
    string * arr;
    const static int front = 0;
    int back;
    int capacity;
    int usedCapacity;
};


Queue.cpp
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
80
81
82
83
84
85
86
87
88
89
90
#include "Queue.h"

Queue::Queue()
{
    back = -1;
    capacity = 1;
    usedCapacity = 0;
    arr = new string[capacity];
}

Queue::Queue(const Queue & source)
{
    cout << "Invoking copy constructor" << endl;
    this->back = source.back;
    this->capacity = source.capacity;
    this->usedCapacity = source.usedCapacity;
    arr = new string[source.capacity];
    copy(source.arr, source.arr + usedCapacity, this->arr);
}

Queue::~Queue()
{
    delete[] arr;
}

Queue & Queue::operator=(const Queue & source)
{
    if (this == &source)
        return *this;
    cout << "Invoking Assignament operator" << endl;
    Queue temp(source);
    swap(temp.back, back);
    swap(temp.capacity, capacity);
    swap(temp.usedCapacity, capacity);
    swap(temp.arr, arr);

    return *this;
}

void Queue::enqueue(string value)
{
    ++back;
    arr[back] = value;
    usedCapacity++;
    memory(usedCapacity);
}

string Queue::dequeue()
{
    string returnValue = arr[front];
    shift();
    usedCapacity--;
    memory(usedCapacity);
    return returnValue;
}

void Queue::showFullQueue() const
{
    cout << "Front: " << front << endl;
    cout << "Back: " << back << endl;
    for (int i = 0; i < capacity; i++)
    {
        cout << arr[i] << ", ";
    }
    cout << endl;
}

void Queue::memory(int currentCapacity)
{
    if (currentCapacity >= capacity)
    {
        int newCapacity = (currentCapacity * 3) / 2 + 1;
        string * arr2 = new string[newCapacity];
        copy(arr, arr + usedCapacity, arr2);
        delete[] arr;
        arr = arr2;
        capacity = newCapacity;
    }
    else
        cout << "allocation error";
}

void Queue::shift()
{
    for (int i = front; i <= back; i++)
    {
        arr[i] = arr[i + 1];
    }
    --back;
}


main.cpp
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
#include "Queue.h";

int main()
{
    Queue names;
    names.enqueue("A");
    names.enqueue("B");
    names.enqueue("C");
    names.dequeue();
    names.enqueue("D");
    names.enqueue("E");
    names.enqueue("F");
    names.enqueue("G");
    names.enqueue("H");
    names.enqueue("I");
    names.enqueue("J");
    names.enqueue("K");
    names.dequeue();
    names.dequeue();
    names.dequeue();
    names.enqueue("W");
    names.enqueue("X");

    names.showFullQueue();

    Queue names2;
    names2 = names;
    names2.showFullQueue();

    names2.enqueue("Tom");
    names2.enqueue("Alice");
    names2.enqueue("Josh");
    names2.enqueue("Bridget");

    names2.showFullQueue();



    return 0;
}
well, I seem to have fixed my problem.
Last edited on
Topic archived. No new replies allowed.