2

The following gives an error for the code mentioned below. Where I have gone wrong ?

error: ‘function’ is not a member of ‘std’

I want to make priority queue using C++ std lib queue, and min the queue is that IceCream which takes least time to prep. I have tried implementing this -> declaring a priority_queue in c++ with a custom comparator

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <iterator>
#include <string>
#include <sstream>

using namespace std;


class IceCream
{
 public:
   int time_to_prep;
    IceCream(int flav) {
          time_to_prep = flav;
       }
};

bool Compare(IceCream a, IceCream b)
{
    return a.time_to_prep > b.time_to_prep;
}

int main()
{
    priority_queue<IceCream, vector<IceCream>, std::function<bool(IceCream a, IceCream b)> > my_pq(Compare);
    my_pq.push(IceCream(4));
    my_pq.push(IceCream(33));
    my_pq.push(IceCream(9));
    cout << my_pq.top() << endl;
    return 0;
}
Community
  • 1
  • 1
Mohit Kumar
  • 500
  • 6
  • 24

1 Answers1

6
#include <functional>

You need this include to get access to std::function

See: http://en.cppreference.com/w/cpp/utility/functional/function

sji
  • 1,877
  • 1
  • 13
  • 28
  • it stops at the last line.. ERROR '`error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘const value_type {aka const IceCream}’) cout << my_pq.top() << endl;` – Mohit Kumar Oct 11 '16 at 14:21
  • 1
    @MohitKumar If by that you mean there there is another compile error on the last line, that is because you are trying to print `my_pq.top()` which returns an instance of `IceCream` which cannot be printed. You need to do `cout << my_pq.top().time_to_prep << endl` – sji Oct 11 '16 at 14:22
  • @MohitKumar That's a different issue. – πάντα ῥεῖ Oct 11 '16 at 14:23