我想通过使用STL优先级队列根据它们的优先级实现一系列杂事。当我尝试编译时,我收到了有关输出函数的错误以及过载的运算符<。我试着让函数为const,但是这并没有解决问题。我怎样才能解决这个问题?如何解决这个丢弃限定符错误?
main.cc
priority_queue<Chore> chores;
Chore tmp;
for(int i = 5; i >0; i--) {
tmp.input(cin);
chores.push(tmp);
}
while(!chores.empty()) {
chores.top().output();
chores.pop();
}
return 0;
};
chore.h
class Chore {
public:
Chore();
void input(istream& ins);
void const output();
bool const operator <(const Chore c1);
private:
string chore_name;
int priority;
};
chore.cc
Chore:: Chore() {
chore_name = "";
priority = 0;
};
void Chore:: input(istream& ins) {
cout << "Please Enter the name of the chore: ";
cin >> chore_name;
cout << endl << "Please Enter The Priority Level: ";
cin >> priority;
cout << endl;
};
void const Chore:: output() {
cout << "Chore: " << chore_name << endl;
cout << "Priority: " << priority << endl << endl;
};
bool const Chore:: operator <(const Chore c1) {
return (priority < c1.priority);
};
什么是编译器错误? – user463035818
我对你的代码有点困惑。不应该是'void Chore :: output()const {...'? – user463035818