0

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"

3
  • TYPE dummy = 0; is no good if TYPE is a std::string - make it TYPE dummy (still questionable, but better) Commented Nov 17, 2014 at 20:06
  • @DieterLücking TYPE dummy{}; or TYPE dummy = TYPE();. Commented Nov 17, 2014 at 20:09
  • @juanchopanza: Better the first one, who knows whether TYPE is moveconstructible / copyconstructible. (even though RVO applies) Commented Nov 17, 2014 at 20:11

1 Answer 1

2
template<class TYPE>
Heap<TYPE>::Heap(){
    TYPE dummy = 0;
    heap.push_back(dummy);
    size = heap.size() - 1;
}

The first line would translate to:

 std:string dummy = 0;

I don't think you can set a string to zero.

try changing it to:

TYPE dummy = TYPE();

UPDATE: as juanchopanza points out, the modern syntax is:

TYPE dummy{}; 

(I'm still living in the past...)

Sign up to request clarification or add additional context in comments.

3 Comments

Correct. "setting" a string to zero invokes the const char* constructor, which assumes a pointer to a null-terminaded string.
Another quick question, changing that in the code allowed it to create the heap as a string. Now I get several errors saying "No constructor could take the source type, or constructor overload resolution was ambiguous", do I need to make more constructor classes for the string cases? I thought that was the whole idea behind a template
Where are you getting those errors? (post it as a new question)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.