我有一個類,像這樣:不能分配給該
class Node {
// data
Node* next;
void operator++(int);
};
當我定義遞增運算符,就像這樣:
void Node::operator++(int) {
this = this->next;
}
我得到的錯誤Expression is not assignable
上this = this->next;
。
這是怎麼回事?我如何獲得this
指向next
?
'this'只能用作指向當前實例的指針。指派給「this」是沒有意義的。你爲什麼想這麼做 ? – Tuxdude 2013-03-11 07:20:45
也許你可以改成'Node * Node :: operator ++(int){return this-> next; }' – Jichao 2013-03-11 07:22:34
順便說一句,後綴'operator ++'返回void是很奇怪的。這勢必會混淆用戶。人們會期望它返回一個「節點」。 – juanchopanza 2013-03-11 07:25:42