I can't get the Heap class to accept the Comparator as the second template argument. The compiler keeps trying to instantiate a Comparator object. Why? All I want is a comparision between the 2 provided inputs. Here's the compiler error:
In file included from main.cpp:1:0:
Heap.hpp: In instantiation of ‘void Heap<T, Cmp>::bubbleUp(size_t) [with T = long unsigned int; Cmp = Comparator<long unsigned int>; size_t = long unsigned int]’:
Heap.hpp:29:35: required from ‘void Heap<T, Cmp>::insert(const T&) [with T = long unsigned int; Cmp = Comparator<long unsigned int>]’
main.cpp:7:15: required from here
Heap.hpp:119:9: error: no matching function for call to ‘Comparator<long unsigned int>::Comparator(__gnu_cxx::__alloc_traits<std::allocator<long unsigned int> >::value_type&, __gnu_cxx::__alloc_traits<std::allocator<long unsigned int> >::value_type&)’
if (Cmp(fArray[idx], fArray[getParent(idx)]))
^
Heap.hpp:119:9: note: candidates are:
Heap.hpp:128:7: note: constexpr Comparator<long unsigned int>::Comparator()
class Comparator
Here's the class
#pragma once
#include <vector>
#include <functional>
#include <assert.h>
#include "boost/optional.hpp"
template <typename T, typename Cmp>
class Heap
{
public:
Heap()
: fArray()
{
}
~Heap() {}
void insert(const T &toInsert)
{
fArray.push_back(toInsert);
bubbleUp(fArray.size() - 1);
}
private:
std::vector<T> fArray;
size_t getParent(size_t i) const
{
assert(i / 2 < fArray.size());
return i / 2;
}
void bubbleUp(size_t idx)
{
if (idx == 0)
{
return;
// return early if root
}
// If heap property violated, swap and recurse upwards
if (Cmp(fArray[idx], fArray[getParent(idx)])
{
std::iter_swap(fArray.begin() + idx, fArray.begin() + getParent(idx));
bubbleUp(getParent(idx));
}
}
};
Here's the comparator:
template <typename T>
class Comparator
{
public:
bool operator()(const T &o1, const T &&o2)
{
return o1 < o2;
}
};
Here's the main function
int main(){
Heap< size_t,Comparator<size_t> > h;
h.insert(4);
h.insert(5);
h.insert(6);
h.insert(3);
}