I am working on a heap implementation. It must be a template class, and I need it to have its own comparator passed in the constructor. How can i do this?
I tried this:
template <typename T>
class Heap{
public:
Heap(int size, bool (*comparator) (const T & a, const T & b) = [] (const T & a, const T & b){
return a < b;
})
}
What works for:
Heap<int> heap(4);
And also for:
Heap<int> heap(4, [](const int & a, const int & b){ return false; })
But when I try to use it with pointers like this (where a sentence is some structure):
Heap<Offer*> heap2(3, [](Offer const * a, Offer const * b){
return false;
});
I got this compilation error:
test.cpp: In function ‘int main()’:
test.cpp:126:3: error: invalid user-defined conversion from ‘main()::<lambda(const Offer*, const Offer*)>’ to ‘bool (*)(Offer* const&, Offer* const&)’ [-fpermissive]
});
^
test.cpp:124:59: note: candidate is: main()::<lambda(const Offer*, const Offer*)>::operator bool (*)(const Offer*, const Offer*)() const <near match>
Heap<Offer*> heap2(3, [](Offer const * a, Offer const * b){
^
test.cpp:124:59: note: no known conversion from ‘bool (*)(const Offer*, const Offer*)’ to ‘bool (*)(Offer* const&, Offer* const&)’
test.cpp:13:5: note: initializing argument 2 of ‘Heap<T>::Heap(int, bool (*)(const T&, const T&)) [with T = Offer*]’
Heap(int size, bool (*comparator) (const T & a, const T & b) = [] (const T & a, const T & b){
How to make it work for both scenarios?
source
share