I get a runtime error when I add a destructor to my code.
Code:
#include <iostream>
#include <array>
using namespace std;
struct nodes { int* elements; };
class heap
{
private:
int length;
nodes item;
public:
heap();
//~heap();
void heap_size(int num);
void push_heap(int item);
void maxHeapify(int parent,int last);
void print_heap();
};
heap::heap() { }
/*heap::~heap()
{
delete [] item.elements;
}*/
//setting size of the array and making a new one
void heap::heap_size(int max)
{
item.elements = new int [max];
}
//pushing numbers into the array
void heap::push_heap(int number)
{
length++;
item.elements[length-1] = number;
}
//sorting the array
void heap::maxHeapify(int parent, int last)
{
int child = 2 * parent;
while (child <= last)
{
if (child + 1 <= last && item.elements[child+1] > item.elements[child])
child++;
if (item.elements[child] > item.elements[parent])
swap(item.elements[child], item.elements[parent]);
parent = child;
child = 2*parent;
}
}
void heap::print_heap()
{
for (int i=0; i<length; ++i)
{
cout<<"element["<<i<<"]"<<item.elements[i]<<",";
}
}
int main(int argc, const char * argv[])
{
cout<<"How many numbers do you want to enter into the heap?"<<endl;
int amount;
cin>>amount;
heap myHeap;
myHeap.heap_size(amount); //setting the size of the array
int numbers=0;
cout<<"Enter "<<amount<<" numbers into the heap and press enter to see the results printed."<<endl;
for (int i=1; i<=amount; ++i)
{
cin>>numbers;
myHeap.push_heap(numbers); //push numbers into the array
}
myHeap.maxHeapify(0, amount);
myHeap.print_heap();
// myHeap.~heap();
return 0;
}