Created a template class that complies and runs fine with integers, when i attempt to run with strings I receive invalid null pointer error.
I only added in the methods where the heap is created, which is where the error was found.
//heap.h
#include <iostream>
#include <vector>
using namespace std;
template<class TYPE>
class Heap{
private:
vector<TYPE> heap;
int size;// number of elements in the heap
bool maxheap = true;//default value
TYPE bubble_up(TYPE item);
TYPE bubble_down(TYPE item);
public:
Heap();
Heap(bool maxheap);
Heap(vector<TYPE>, bool order);
~Heap();
void build_heap();
TYPE Insert(TYPE item);
TYPE Delete(TYPE& item);
const vector<TYPE> sort(bool order);
const vector<TYPE> sort();// defualt sort if no variable given, max sort
TYPE get_size();
void print_heap();
void clear_heap();
};
template<class TYPE>
Heap<TYPE>::Heap(){
TYPE dummy = 0;
heap.push_back(dummy);
size = heap.size() - 1;
}
template<class TYPE>
Heap<TYPE>::Heap(bool order){
maxheap = order; // true is max, false is min
TYPE dummy = 0;
heap.push_back(dummy);
size = heap.size() - 1;
}
template<class TYPE>
Heap<TYPE>::Heap(vector<TYPE> x, bool order){
maxheap = order;// true is max, false is min
TYPE tempSize;
TYPE dummy = 0;
heap.push_back(dummy);
size = heap.size() - 1;
tempSize = x.size();
for (TYPE y = 0; y < tempSize; y++){
heap.push_back(x[y]);
}
size = heap.size() - 1;
build_heap();
}
I have cut out the part where the issue was found in the next few lines of code.
//driver.cpp
#include <iostream>
#include <string>
#include "Heap.h"
using std::cout;
using std::endl;
typedef string TYPE;
int main(void) {
Heap<std::string> *s_heap = new Heap<std::string>(); // string heap
std::string s_item = "0";
}
"Debug assertion failed!" "Expression: Invalid null pointer"
TYPE dummy = 0;is no good if TYPE is a std::string - make itTYPE dummy(still questionable, but better)TYPE dummy{};orTYPE dummy = TYPE();.TYPEis moveconstructible / copyconstructible. (even though RVO applies)