我一直在尝试为我的Linked List实现创建迭代器。但我收到一个错误。我的实现如下:
#ifndef LinkedListIterative_h
#define LinkedListIterative_h
#include <cstdio>
using std::swap;
template <typename T>
class LinkedList;
template <typename T>
class Iterator;
template<typename T>
class Node
{
private:
friend class Iterator<T>;
friend class LinkedList<T>;
T data;
Node *next;
Node() : next(nullptr) {}
Node(const T& data) : data(data) {}
};
template<typename T>
class Iterator
{
private:
Node<T> *node;
public:
Iterator() : node(nullptr) {}
Iterator(Node<T> *node) : node(node) {}
Iterator(const Node<T> *node) : node(node) {}
Iterator(const Iterator& iterator) : node(iterator.node)
{
}
Iterator& operator= (const Iterator& rhs)
{
if( this != &rhs )
{
node = rhs.node;
}
}
Iterator& operator++()
{
node = node->next;
return *this;
}
Iterator& operator+(size_t index)
{
while( index-- > 0 && (node != nullptr) )
{
++this;
}
return *this;
}
Iterator& operator[](size_t index)
{
while( index-- > 0 && (node != nullptr) )
{
++this;
}
return *this;
}
bool operator==(const Iterator &iter)
{
return node == iter.node;
}
bool operator!=(const Iterator &iter)
{
return node != iter.node;
}
T& operator*() { return node->data; }
};
template <typename T>
class LinkedList
{
private:
size_t size;
Node<T> *first;
Node<T> *last;
Node<T>* createNode(const T &item)
{
Node<T> *node = new Node<T>;
node->data = item;
node->next = nullptr;
return node;
}
LinkedList(const LinkedList& list){}
LinkedList& operator=(const LinkedList& list){}
public:
typedef Iterator<T> iterator;
typedef Iterator<const T> const_iterator;
LinkedList() : size(0), first(nullptr), last(nullptr)
{
}
// Add item at the end of the list
void add(const T& item)
{
Node<T> *node = createNode(item);
if(first == nullptr)
{
first = node;
last = node;
}
else
{
last->next = node;
last = node;
}
++size;
}
void add(const T& item,size_t index)
{
if( size == 0 )
{
add(item);
}
else if( index == 0 && size > 0 )
{
addToFirst(item);
}
else if( index >= size )
{
addToLast(item);
}
else
{
Node<T> *prev = first;
Node<T> *curr = first;
int i = 0;
while( i++ < index )
{
prev = curr;
curr = curr->next;
}
Node<T> *node = createNode(item);
prev->next = node;
node->next = curr;
++size;
}
}
void addToFirst(const T& item)
{
Node<T> *node = createNode(item);
node->next = first;
first = node;
if(size == 0)
last = node;
++size;
}
void addToLast(const T& item)
{
Node<T> *node = createNode(item);
last->next = node;
last = node;
if(size == 0)
first = node;
++size;
}
void removeFirst()
{
if( first == nullptr )
return;
Node<T> *temp = first;
first = first->next;
--size;
delete temp;
if( size == 0 )
last = first;
}
void remove(const size_t index)
{
if( size == 0 || index > size - 1 )
throw std::out_of_range("Out of range");
if(index == 0)
{
removeFirst();
return;
}
Node<T> *curr = first;
Node<T> *prev = first;
size_t i(0);
while( i++ < index )
{
prev = curr;
curr = curr->next;
}
if( curr == last )
{
last = prev;
}
prev->next = curr->next;
delete curr;
--size;
}
void removeLast()
{
if( first == nullptr )
return;
Node<T> *curr = first;
Node<T> *prev = first;
while( curr != last )
{
prev = curr;
curr = curr->next;
}
prev->next = nullptr;
delete last;
last = prev;
--size;
if( size == 0 )
first = last;
}
T& getItem(size_t index) const
{
if(index > size)
throw std::out_of_range("index out of bound!");
Node<T> *curr = first;
size_t i = 0;
while ( i++ != index )
curr = curr->next;
return curr->data;
}
size_t length() const
{
return size;
}
iterator begin()
{
return iterator(first);
}
iterator end()
{
return iterator(last); //getting error here
}
const_iterator begin() const
{
return const_iterator(first);
}
const_iterator end() const
{
const_iterator(last);
}
~LinkedList()
{
Node<T> *curr = first;
while( curr != last )
{
Node<T> *temp = curr;
curr = curr->next;
delete temp;
}
}
};
#endif /* LinkedListIterative_h */
我得到的错误是:
Error C2440 '<function-style-cast>': cannot convert from 'Node<T> *const ' to 'Iterator<const T>'
这个内部主要的来电者是:
void printList(const LinkedList<int>& list)
{
size_t index = 0;
LinkedList<int>::const_iterator iter = list.begin();
for(; iter != list.end(); ++iter)
{
std::cout << *iter << " ";
}
while( index < list.length() )
{
std::cout << list.getItem(index ++) << " ";
}
std::cout << std::endl;
}
我无法弄清楚什么是错的。
答案 0 :(得分:1)
您在const_iterator begin() const
和const_iterator end() const
方法中收到错误,因为您尝试将指针传递给Node<T>
而const_iterator
构造函数接受指向Node<const T>
的指针。你似乎搞砸了const
位置。尝试更改迭代器类,以便mutable和const变量都接受指向(可能是const限定的)Node<T>
的指针。
此外const_iterator end() const
缺少退货声明;
答案 1 :(得分:1)
因为您的const_iterator是Iterator<const T>
类型,而转换构造函数Iterator(const Node<const t> *)
缺失。
老实说,当数据类型与节点对应时,我认为定义这样的const_iterator是正确的。因为通过这种方式,您将拥有两种不同的节点类型:Node<T>
和Node<const T>
。