Vous n'implémentez pas operator++
pour la classe de nœud; vous l'implémentez pour l'itérateur. La classe itérateur doit être une classe distincte.
Et s'il vous plaît, ne pas gâcher votre modèle en faisant des hypothèses (depuis val
est un T
, votre constructeur devrait accepter un T
, pas int
). De plus, n'ignorez pas le paramètre int
de l'opérateur ++ comme cela: c'est un mannequin utilisé pour distinguer l'implémentation de pré-incrémentation de l'implémentation post-incrément.
template <typename T>
struct Node {
T val;
Node *next;
Node(const T& t = T()) : val(t) {}
};
template <typename T>
struct node_iter {
Node<T>* current;
node_iter(Node<T>* current): current(current) {}
const node_iter& operator++() { current = current->next; return *this; }
node_iter operator++(int) {
node_iter result = *this; ++(*this); return result;
}
T& operator*() { return current->val; }
};
int main() {
// We make an array of nodes, and link them together - no point in
// dynamic allocation for such a simple example.
Node<int> nodes[10];
for (int i = 0; i < 10; ++i) {
nodes[i] = Node<int>(i);
nodes[i].next = (i == 9) ? nodes + i + 1 : 0;
}
// we supply a pointer to the first element of the array
node_iter<int> test(nodes);
// and then iterate:
while (test.current) {
cout << *test++ << " ";
}
// Exercise: try linking the nodes in reverse order. Therefore, we create
// 'test' with a pointer to the last element of the array, rather than
// the first. However, we will not need to change the while loop, because
// of how the operator overload works.
// Exercise: try writing that last while loop as a for loop. Do not use
// any information about the number of nodes.
}
Ceci est encore un long, loin de fournir l'encapsulation des données, une gestion de la mémoire, etc. Faire une classe de liste chaînée est correcte pas facile. C'est pourquoi la bibliothèque standard en fournit un. Ne réinventez pas la roue.
Vous ne pouvez pas. head est un pointeur et l'opérateur ++ est intégré/défini pour les pointeurs. Si la tête était un objet ou une référence à un objet, alors vous pourriez le faire. –
hmm..okay. Connaissez-vous des liens vers l'implémentation d'itérateurs? Merci vous – blueskin
Vous pouvez regarder les réponses à cette question: http://stackoverflow.com/questions/3582608/how-to-correctly-implement-custom-iterators-and-const-iterators –