0xC0000005 Error for my PriorityQueue template

So, like the title says I'm getting a
Process returned -1073741819 (0xC0000005)
error for a Priority Queue template I've been working on.
Here's the h. file

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef PRIORITYQUEUE_H_INCLUDED
#define PRIORITYQUEUE_H_INCLUDED
#include <algorithm>

using namespace std;

template<typename V>
class PriorityQueue
{
    V* values;
    int cap;
    int siz;
    void capacity(int);

public:
    PriorityQueue(int = 2);
    PriorityQueue(const PriorityQueue<V>&);
    ~PriorityQueue() {delete [ ] values;}
    PriorityQueue<V>& operator=(const PriorityQueue<V>&);

    void push(const V&);
    void pop();
    V top() const {return values [0];}
    int size() const{return siz;}
    bool empty() const {return siz == 0 ? true : false;}
    void clear() {siz = 0;}
};

template<typename V> //capacity setter
void PriorityQueue<V>::capacity(int)
{
    V* temp = new V[cap];
    int limit = (cap < this->cap ? cap : this->cap);
    for(int i = 0; i < limit; i++)
        temp[i] = values[i];

    for(int i = limit; i < cap; i++)
        temp[i] = V();

    delete [ ] values;
    values = temp;
    this->cap = cap;
}

template<typename V>//default constructor
PriorityQueue<V>::PriorityQueue(int)
{
    siz = 0;
    cap = 2;

    values = new V[cap];
}

template<typename V>//copy constructor
PriorityQueue<V>::PriorityQueue(const PriorityQueue<V>& original)
{
    cap = original.cap;
    siz = original.siz;
    values = new V[cap];

    for (int i = 0; i < cap; i++)
        values[i] = original.values[i];
}

template<typename V>//assignment operator
PriorityQueue<V>& PriorityQueue<V>::operator=(const PriorityQueue<V>& original)
{
    if (this != &original)
    {
        delete [ ] values;

        siz = original.siz;
        cap = original.cap;
        values = new V[cap];
        for(int i = 0; i < cap; i++)
            values[i] = original.values[i];
    }
    return *this;
}

template<typename V>
void PriorityQueue<V>::push(const V& value)
{
    if(siz == cap)
        capacity(2*cap);

    values[siz] = value;
    int index = siz;
    int parentIndex = ((index + 1)/2) - 1;
    siz++;

    while(parentIndex >= 0)
    {
        if(values[index] > values[parentIndex])
        {
            swap(values[index], values[parentIndex]);
            index = parentIndex;
            parentIndex = ((index + 1)/2) - 1;
        }
        else break;
    }
}

template<typename V>
void PriorityQueue<V>::pop( )
{
    if(siz == 0) return;
    siz--;
    values[0] = values[siz];
    int index = 0;

    while(true)
    {
        int leftIndex = (index*2) + 1;
        int rightIndex = (index*2) + 2;

        if(leftIndex >= siz) //no children
            break;
        else if(leftIndex < siz && rightIndex >= siz)//only one child
        {
            if(values[leftIndex] > values[index])
            {
                swap(values[leftIndex], values[index]);
                continue;
            }
            else break;
        }
        else if(rightIndex < siz)//Has both child nodes
        {
            if(values[leftIndex] >= values[rightIndex])
            {
                if(values[leftIndex] < values[index])
                    break;
                swap(values[leftIndex], values[index]);
                index = leftIndex;
            }
            else if(values[rightIndex] > values[index])
            {
                swap(values[rightIndex], values[index]);
                index = rightIndex;
            }
            else break;
        }
    }
}
#endif // PRIORITYQUEUE_H_INCLUDED 

And the 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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "PriorityQueue.h"

using namespace std;

int main()
{

    int n = 500;

    srand(time(0));
    PriorityQueue<int> a(n);

    //////Start of Array//////
    for(int i = 0; i < n; i++)
        {
            int Rand = rand() % 101;
            a.push(Rand);
        }

    for(int i = 0; i < n; i++)
        {
            cout << a.top() << " ";
            a.pop();
        }

    
    return 0;
}


I've been troubleshooting it for a couple days now and I can't for the life of me find where the problem lies. I THINK I've narrowed it down to the push and/or pop member functions. Any help is appreciated
Last edited on
uhmmmm... anyone else having trouble using the code tags???
Welp. I finally solved it. If, for some reason, someone else is having this issue my default constructor was the issue. It works if it's written as:

1
2
3
4
5
6
7
8
template<typename V>//default constructor
PriorityQueue<V>::PriorityQueue(int cap)
{
    siz = 0;
    this->cap = cap;

    values = new V[cap];
}
Topic archived. No new replies allowed.