Priority Queue's always make const?

So I was hoping someone might be able to lend me a hand with this. About a month ago I was given my final exam for one of my programming classes (which has finished now) where we were given several options of programs to code. One of the ones I chose to work on needed me to create a program that would read in several lines of data that included a ID code (C for corporate, M for Military, and U for US Citizen), followed by an alpha numeric string for what the situation was (for display only), a severity code ranging from 0-10, and a brief description of the problem. My professor's example for the cod was “M,E302, 9, POTUS has lost his ducky”. We were then to read these lines in one at a time and have them placed into a priority queue based on severity (M>C>U, 9>0 etc), and then display the new organized list to the console. Seemed simple enough. For that I made each of the lines into its own class/object.

The issue I kept running into (other than having never used a priority queue before) is that for some reason my code would error out when I tried to display the newly built queue, claiming that the Alert objects after being inserted into the priority queue were changed into const data type (const alert), and as a result of being const I could no longer use the Display function inside of the objects. I tried playing around with this for hours using any technique I could find but I just couldn't fix the issue. I'm not sure if this is just some problem with Priority queues I was not aware of or if I royally screwed something up. Any help would be appreciated, as I would like to use priority queue's in the future, and this is also not the first time I've had something randomly pop up as a const.

alert.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifndef ALERT_H
#define ALERT_H
#include <iostream>
#include <cmath>
#include <random>
#include <ctime>
#include <string>
#include <fstream>
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

using namespace std;

class alert
{
public:
    alert();
    alert(char c, string i, int p, string d)
    {
        cat = c;
        id = i;
        pri = p;
        desc = d;

        if (c == 'M')
            catPri = 3;
        else if (c == 'C')
            catPri = 2;
        else catPri = 1;
    }



    void display()
    {
        cout << cat << " " << id << " " << pri << " " << desc << endl;
    }

    int getCatPri()
    {
        return catPri;
    }

    int getPri()
    {
        return pri;
    }
    char getCat()
    {
        return cat;
    }
    string getID()
    {
        return id;
    }
    string getDe()
    {
        return desc;
    }
private:
    char cat;
    string id;
    int catPri;
    int pri;
    string desc;
};


class Compare
{
public:
    bool operator() (alert a, alert b)
    {
        if (a.getCatPri() >= b.getCatPri() && a.getPri() >= b.getPri())
            return true;
    }
};

#endif 


ducky.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "alert.h"
#include <queue>

int main (int argc, char** argv)
{
    if (argc != 2)
    {
        cerr << "Usage: " << argv[0] << "inFileName" << endl;
        return -1;
    }

    alert * aptr;
    char cat;
    string id;
    int pri;
    string desc;
    ifstream data (argv [1]);
    priority_queue<alert, vector<alert>, Compare> qu;
    while (data)
    {

        data >> cat >> ws;
        data >> id >> ws;
        data >> pri >> ws;
        getline(data, desc);

        aptr = new alert(cat,id, pri, desc);
        qu.push(*aptr);
    }


    while (!qu.empty())
    {
         alert * disp;
        disp = &qu.top();
        //vec.pop();
        if (qu.empty())
            {break;}
        disp->display();


    }

    return 0;
}

Did you try to const qualify display()?

1
2
3
4
    void display() const
    {
        cout << cat << " " << id << " " << pri << " " << desc << endl;
    }


Also you have several other functions that probably should be const qualified. Look for functions that don't alter the state of the class, like display().

Why does alert.h have all those #include files? You should only #include the necessary #include files required to compile the code.

The reason for all the headers is because I was lacking time. There were a few issues with my finals where all of them got compounded into very small time slots, and I needed to write several programs for this class. The previous one I had finished I had run into a terrible bug that ate up my time so I didn't have much left by the time I finished it... SO I simply copied over all the headers from that because i didn't know at the time what I would and wouldn't need (again wasn't too sure about how custom queues worked, better to be prepared) and simply forgot to delete the unneeded headers...

Anyway I didn't actually know that const qualifying was a thing. In my classes the use of const is so rare that I barely have any experience with it. I'll try that out and see what happens.
Topic archived. No new replies allowed.