問題1:c++運算符重載爲類的非靜態成員

遭遇的問題是:

原代碼爲:

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約束。

 

箴言錄:

與人不求備,檢身若不及。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章