I am working on creating a priority queue of a class that I have created called Chore. The class just holds the name of a given chore and the priority level of that chore, where a higher number means higher priority. I tested the class with a normal queue and everything worked fine, so I'm guessing the problem lies within my operator overload. The main function creates 5 Chore objects and pushes them into the queue and then outputs the queue in order.
Chore class:
#include <string>
#include <iostream>
class Chore{
public:
Chore(std::string s = "N/A", int i = 0){chore = s; pnumber = i;}
friend bool operator <(Chore c1, Chore c2){
return c1.pnumber < c2.pnumber;
}
void input(){
std::cout << "Enter type of chore: ";
std::cin >> chore;
std::cout << "Enter priority level: ";
std::cin >> pnumber;
}
void output(){
std::cout << "Chore: " << chore << std::endl << "Priority level: " << pnumber << std::endl;
}
private:
std::string chore;
int pnumber;
};
main function:
#include <queue>
#include "chore.h"
using namespace std;
int main(){
priority_queue<Chore> q;
Chore temp;
for(int i = 0; i < 5; i++){
temp.input();
q.push(temp);
}
cout << endl;
for(int i = 0; i < 5; i++){
q.top().output();
q.pop();
}
}
The error I am getting upon compiling is:
error: passing 'const Chore' as 'this' argument discards qualifiers [-fpermissive]
q.top().output();
Is the problem my overloaded operator? How can I fix this?
Your output
function needs to be const
void output() const {
std::cout << "Chore: " << chore << std::endl << "Priority level: " << pnumber << std::endl;
}