0

I'm trying to insert a number into my vector "heapvect." I keep changing from references and pointers and it still won't allow me to pass in num into the vector. I have made this without template and making a dynamic array (constructor/destructor), I'm unsure if having a vector and template made me think differently of the whole heap sort.

I tried having pointers for heapvect and getheapvect.

//main.cpp
#include"heap.h"
#include<iostream>
#include<vector>
using namespace std;
int main() {
    int size;
    int num;
    cout << "USER:" << endl;
    cout << "Enter size for heap: " << endl;
    cin >> size;
    heap<class T> yeet;

    cout << "Enter in " << size << " of items:" << endl;
    for (int i = 0; i < size; i++) {
        cin >> num;
        yeet.getheapvect().push_back(num);
    }
    system("pause");
    return 0;
}



//heap.cpp
#include "heap.h"
template<class T>
heap<T>::heap() {
}
template<class T>
heap<T>::heap(int size) {
    getheapvect().resize(size*2);
}
template<class T>
heap<T>::~heap() {
}
template<class T>
void heap<T>::insertin(const T item) {
    getheapvect().push_back(item);
    int index = distance(getheapvect().begin(), item);
    while (index != 0 && getheapvect()[parent(index)] > getheapvect()[index]) {
        swap(getheapvect[index], getheapvect()[parent(index)]);
        index = parent(index);
    }
}
template<class T>
void heap<T>::minheap(int size, int index) {
    int large = index;
    int left = (2 * index) + 1;
    int right = (2 * index) + 2;

    if (left < size && getheapvect()[left]>getheapvect()[large])
        large = left;
    if (right < size && getheapvect()[right]>getheapvect()[large])
        large = right;
    if (large != index) {
        swap(getheapvect()[index], getheapvect()[large]);
        makeheap(size, large);
    }
}
template<class T>
void heap<T>::heapsort(int size) {
    for (int i = (size / 2) - 1; i >= 0; i--)
        makeheap(size, i);
    for (int i = size - 1; i >= 0; i--) {
        swap(getheapvect()[0], getheapvect()[i]);
        makeheap(i, 0);
    }
}
template<class T>
void heap<T>::print(int size) {
    for (int i = 0; i < size; i++)
        cout << getheapvect()[i] << ' ';
    cout << endl;
}
template<class T>
void heap<T>::PRINT() {
    print(getheapvect().size());
}
template<class T>
void heap<T>::swap(T  a, T  b) {
    T temp = a;
    a = b;
    b = temp;
}



//heap.h
#ifndef HEAP_H
#define HEAP_H
#include<iostream>
#include<vector>
using namespace std;

template<class T>
class heap {
private:
    vector<T> heapvect;
    void print(int size);
public:
    heap();
    heap(int size);
    ~heap();
    void insertin(const T item);
    void minheap(int size, int index);
    void heapsort(int size);
    void PRINT();
    void swap(T a, T b);
    int parent(int i) { return ((i - 1) / 2); };
    int leftchild(int i) { return ((2 * i) + 1); };
    int rightchild(int i) { return ((2 * i) + 2); };
    vector<T>& getheapvect(){ return heapvect; };
};
#endif

Output is supposed to bring out a sorted vector in ascending order.

scapegoat
  • 7
  • 2
  • Oh, that's not the answer to your question, but useful nevertheless What exactly are you trying to do in the line `heap yeet;`? – Yksisarvinen Apr 30 '19 at 22:06
  • I don't believe the "duplicate" has the same problems as mine, though it did help a lot. My mistake if it really seems that way. – scapegoat Apr 30 '19 at 22:19
  • Well, yeah. I noticed that after I raised the flag, sorry. I left it here because it seems you will encouter that problem soon. But it seems you got the answer you excepted anyway :) – Yksisarvinen Apr 30 '19 at 22:21

1 Answers1

0

When you are ready to use a class template, you need to provide a type that can be used to instantiate it.

In main, instead of

heap<class T> yeet;

use

heap<int> yeet;
R Sahu
  • 204,454
  • 14
  • 159
  • 270