Я пытался реализовать связанный список, используя unique_ptr
и добавить к нему итератор. Я столкнулся с определенной проблемой. Когда я пытаюсь использовать decltype
в первом элементе списка с помощью моего итератора, я получаю сообщение об ошибке invalid type "T"
.
Может кто-нибудь объяснить, почему возникла эта ошибка?
Ниже приведен пример, который воспроизводит ошибку.
#include <memory>
#include <iostream>
template<class T>
class MyListItr;
template<typename T>
class MyList {
private:
int _size;
friend class MyListItr<T>;
public:
class Node {
private:
T value;
public:
std::unique_ptr<MyList::Node> next{ nullptr };
Node() = delete;
Node(T& value) :value(value), next(nullptr) {};
Node(MyList<T>::Node& ref) :value(ref.value), next(std::move(ref.next)) {};
T getVal()const { return value; };
void setVal(T value) { this->value = value; };
~Node() {};
};
std::unique_ptr<MyList::Node> head;
MyList(const MyList<T>&) = delete;
MyList& operator=(const MyList<T>) = delete;
MyList() :_size(0), head(nullptr) {};
int size()const { return _size; };
void push_front(T);
T pop_front();
T front()const;
MyListItr<T> begin() {
typename MyList<T>::Node* tmp = this->head.get();
return MyListItr<T>::MyListItr(tmp);
};
MyListItr<T> end() { return MyListItr<T>::MyListItr(nullptr); };
typedef MyListItr<T> iterator;
typedef T value_type;
typedef T* pointer;
typedef std::ptrdiff_t difference_type;
typedef T& reference;
typedef size_t size_type;
};
template<typename T>
class MyListItr : public std::iterator<std::output_iterator_tag, T> {
typename MyList<T>::Node* data;
public:
MyListItr(typename MyList<T>::Node* data) :data(data) {}
bool operator!=(MyListItr<T>const& T)const;
MyListItr<T> operator++();
T operator*();
};
template<typename T>
void MyList<T>::push_front(T x) {
std::unique_ptr<MyList::Node>tmp = std::move(this->head);
this->head = std::make_unique<MyList<T>::Node>(MyList<T>::Node(x));
this->head->next = std::move(tmp);
_size++;
};
template<typename T>
T MyList<T>::pop_front() {
if (this->_size == 0)throw std::out_of_range("Range exceeded");
T tmp = this->head.get()->getVal();
this->head = std::move(this->head->next);
_size--;
return tmp;
};
template<typename T>
T MyList<T>::front()const {
return this->head.get()->getVal();
};
template<typename T>
bool MyListItr<T>::operator!=(MyListItr<T>const& Itr)const {
return this->data != Itr.data;
};
template<typename T>
MyListItr<T> MyListItr<T>::operator++() {
return(this->data->next.get());
};
template<typename T>
T MyListItr<T>::operator*() {
return (this->data->getVal());
};
int main() {
MyList<int> l;
l.push_front(4);
std::cout<< *l.begin();
std::cout << decltype(*l.begin());
return 0;
}