遭遇的問題是:
原代碼爲:
struct Node {
int cost;
int city;
int stops;
bool operator>(const Node& n) { return cost > n.cost; }
};
但編譯出錯,問題在於操作符重載。
解決:將操作符重載改爲:
bool operator>(const Node& n) const{ return cost > n.cost; }
原因分析:可能由於該類的對象作爲參數傳入操作符函數時,也需要const約束。
箴言錄:
與人不求備,檢身若不及。