gpt4 book ai didi

C++ C2440 '' : cannot convert from 'Node *const ' to 'Iterator'

转载 作者:搜寻专家 更新时间:2023-10-31 02:11:08 26 4
gpt4 key购买 nike

我一直在尝试为我的链表实现创建迭代器。但我收到一个错误。我的实现如下:

#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>'   

main 中的调用者是:

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;
}

我不知道怎么了。

最佳答案

因为你的const_iterator是一个类型Iterator<const T> ,而转换构造函数 Iterator(const Node<const t> *)丢失了。

老实说,当数据类型对应一个节点时,我认为这样定义一个const_iterator是不正确的。因为这样一来,你就会有两种不同的节点类型:Node<T>Node<const T> .

关于C++ C2440 '<function-style-cast>' : cannot convert from 'Node<T> *const ' to 'Iterator<const T>' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44042143/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com