Search code examples
c++templatespriority-queueabstract-data-type

Priority Queue: C++ Trouble With Templating Item Types


I am making a priority queues with templates, but I am rather new to them and they are causing me trouble. The priority queue worked without templates, but I am trying to make it generic for any item in the queue. Thank you in advance.

I am getting two errors with a note for each error: "candidate template ignored: couldn't infer template argument 'ItemType'"

Here is the code:

int main()
{
    int choice, item, priority;
    PriorityQueue pq; 
    do
    {
        cout<<"1.Insert\n";
        cout<<"2.Delete\n";
        cout<<"3.Display\n";
        cout<<"4.Quit\n";
        cout<<"Enter your choice : "; 
        cin>>choice;
        switch(choice)
        {
        case 1:
            cout<<"Input the item value to be added in the queue : ";
            cin>>item;
            cout<<"Enter its priority : ";
            cin>>priority;
            pq.insert(item, priority);
            break;
        case 2:
            pq.del();
            break;
        case 3:
            pq.display();
            break;
        case 4:
            break;
        default :
            cout<<"Wrong choice\n";
        }
    }
   while(choice != 4);
    return 0;
}

This is the error box:

PQ.cpp:99:16: error: no matching member function for call to 'del'

        pq.del();
        ~~~^~~

PQ.cpp:44:14: note: candidate template ignored: couldn't infer template argument 'ItemType'

    void del()
         ^

PQ.cpp:102:16: error: no matching member function for call to 'display'

        pq.display();
        ~~~^~~~~~~

PQ.cpp:59:14: note: candidate template ignored: couldn't infer template argument 'ItemType'

    void display()
         ^

Solution

  • Change your function calls like so:

    pq.del<int>();
    pq.display<int>();
    pq.insert<int>(item, priority);