我想通過使用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