【发布时间】:2011-04-06 17:11:37
【问题描述】:
我的重载前缀运算符在查找范围时遇到问题。
#ifndef LIST_H
#define LIST_H
#include <iostream>
using namespace std;
template <typename T>
struct node
{
T data;
node* next;
node* prev;
};
template <typename T>
class list
{
public:
class iterator
{
public:
iterator() : inode(0) {}
iterator(node<T>* current) : inode(current) {}
// overload prefix increment operator (++x)
iterator& operator ++();
}
private:
node<T>* inode; //
};
iterator begin();
iterator end();
private:
// pointer to the head of the dclist
node<T>* head;
};
// constructor
template <typename T>
list<T>::list() {
head = new node<T>;
head->next = head;
head->prev = head;
}
template <typename T> // This function needs proper scope resolution
typename list<T>::iterator::iterator& iterator<T>::operator ++() {
inode = inode->next;
return this;
}
// return iterate from beginning //
template <typename T>
typename list<T>::iterator list<T>::begin() {
return list<T>::iterator(head->next);
}
// return iterate to end //
template <typename T>
typename list<T>::iterator list<T>::end() {
return list<T>::iterator(head);
}
#endif
【问题讨论】:
标签: c++ templates scope operator-overloading