问题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约束。

 

箴言录:

与人不求备,检身若不及。

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