Zrobiłem własną klasę kopca, jeśli z góry określę tym danych to wszystko działa, problem pojawia się gdy chcę zmienić to na szablon, jest tu może ktoś tak miły i poprawi mój kod?
heap.h
#pragma once
#include <vector>
template <class T>
extern class heap
{
public:
heap<T>();
void push(T value);
void print();
T pop();
std::vector<T> sort();
private:
std::vector<T> data;
};
heap.cpp
#include "heap.h"
#include <iostream>
#include <sstream>
template <class T>
heap<T>::heap()
{
}
template <class T>
void heap<T>::print()
{
for(int i = 0; i<data.size(); i++)
{
std::clog << data[i] << " ";
}
}
template <class T>
void heap<T>::push(T value)
{
data.push_back(value);
int64_t index = data.size() - 1; //indeks ostatniego elemetu
int64_t parent = (index - 1) / 2;
while (index>0 && data[parent]<value)
{
data[index] = data[parent];
index = parent;
parent = (index - 1) / 2;
}
data[index] = value;
}
template <class T>
T heap<T>::pop()
{
T value;
value = data[0];
data[0] = data[data.size() - 1];
data.erase(data.end()-1);
int64_t size = data.size() - 1;
std::clog << " size: " << size << "\n";
int64_t index = 0;
int64_t index_left = 2 * index + 1;
int64_t index_right = 2 * index + 2;
while(index_left<size || index_right<size)
{
if(data[index] > data[index_left] && data[index]>data[index_right])
{
break;
}
else
{
if(data[index_left]<data[index_right])
{
if (index_right <= size) {
std::swap(data[index], data[index_right]);
index = index_right;
}
}
else
{
if (index_left <= size) {
std::swap(data[index], data[index_left]);
index = index_left;
}
}
}
index_left = 2 * index + 1;
index_right = 2 * index + 2;
}
return value;
}
template <class T>
std::vector<T> heap<T>::sort()
{
std::vector<T> output;
int64_t size = data.size();
for(int64_t it = 0; it<size; it++)
{
output.push_back(pop());
}
return output;
}
Gdy próbuję to skompilować dostaję całą masę błędów wszystkie są takie:
-Severity Code Description Project File Line Suppression State Error C2027 use of undefined type 'std::_Locinfo'
Orientuję w szablonach funkcji, ale z klasą mam problem, z góry dziękuję za pomoc :)