c++ - linked list with unique pointers being used as type -
i'm having problem getting linked list (it's square list) passing tests have been given professor, , i'm not sure i'm supposed do.
here's code:
/** linkedlist class declaration. */ template <typename t> class linkedlist; template <class tnode> class iterator { /* helper class provide pointer facilities around node */ friend class linkedlist<typename tnode::value_type>; tnode* pnode; //the node oriented instance of iterator. //iterator(tnode* _pnode) : pnode(_pnode) {} public: iterator(tnode* _pnode) : pnode(_pnode) {} using value_type = typename tnode::value_type; //using size_type = std::size_type; using pointer = tnode*; using difference_type = std::ptrdiff_t; using reference = value_type&; using iterator = iterator<tnode>; using iterator_category = std::bidirectional_iterator_tag; .............removed unneeded code............... value_type get() { return pnode->_data; } typename tnode::value_type &operator*(){ return pnode->_data; } }; template <typename t> class node { friend class linkedlist<t>; friend class iterator<node<t> >; node() : _next(0), _prev(0), _head(0), _nexthead(0), _prevhead(0) {} node(t data) : _data(data), _next(0), _head(0), _nexthead(0), _prevhead(0) {} node(t data, node<t>* next, node<t>* prev, node<t>* head, node<t> nexthead, node<t> prevhead) : _data(data), _next(next), _prev(prev), _head(head), _nexthead(nexthead), _prevhead(prevhead){} t _data; node<t>* _next; node<t>* _prev; node<t>* _head; node<t>* _nexthead; node<t>* _prevhead; public: typedef t value_type; }; template <typename t> class linkedlist { public: using size_type = std::size_t; private: node<t>* first; node<t>* last; node<t>* lasthead; size_type _count = 0; double columnnumbers = 0; public: typedef t value_type; using pointer = std::unique_ptr<node<t>>; using iterator = iterator<node<t>>; using difference_type = std::ptrdiff_t; using reference = t&; using const_reference = t const&; using const_pointer = t const*; using const_iterator = iterator const; using reverse_iterator = std::reverse_iterator < iterator >; using const_reverse_iterator = reverse_iterator const; linkedlist() : first(0), last(0), lasthead(0) { } ~linkedlist() { .............removed unneeded code............... } iterator begin(){ return iterator(first); } iterator end(){ return iterator(last); } const_iterator begin() const { return const_iterator(first); } const_iterator end() const { return const_iterator(last); } const_iterator cbegin() const { return const_iterator(first); } const_iterator cend() const { return const_iterator(last); } reverse_iterator rbegin() { return reverse_iterator(last); } reverse_iterator rend() { return reverse_iterator(first); } const_reverse_iterator rbegin() const { return const_reverse_iterator(last); } const_reverse_iterator rend() const { return const_reverse_iterator(first); } const_reverse_iterator crbegin() const { return const_reverse_iterator(last); } const_reverse_iterator crend() const { return const_reverse_iterator(first); } .............removed unneeded code............... void insert(t data) { .............removed unneeded code............... } void reorder() { // reorders head pointers in correct spot square list .............removed unneeded code............... } bool erase(iterator& _inode) //true success, vice versa { .............removed unneeded code............... } void clear() { .............removed unneeded code............... } }; template <typename t> bool operator==(iterator<node<t>> const& lhs, iterator<node<t>> const& rhs){ return lhs.compare(rhs); }
here's test supposed run
boost_auto_test_case(ut_rvalue_insert_scrambled_int) { typedef std::unique_ptr<int> up; std::vector<int> data{ 9, 10, 7, 8, 5, 6, 3, 4, 1, 2 }; linkedlist<up> sqi; (auto datum : data) { sqi.insert(up(new int(datum))); } std::sort(data.begin(), data.end()); std::vector<int> dup; (auto iter = sqi.begin(); iter != sqi.end(); ++iter) { dup.push_back(*iter->get()); } std::sort(data.begin(), data.end()); std::sort(dup.begin(), dup.end()); boost_check(dup.size() == data.size()); boost_check_equal_collections(dup.begin(), dup.end(), data.begin(), data.end()); }
when compiling, these errors:
error 1 error c2819: type 'iterator<node<t>>' not have overloaded member 'operator ->' ut_square_list_10_insert_rvalue.cpp 33
and
error 2 error c2232: '->iterator<node<t>>::get' : left operand has 'class' type, use '.' ut_square_list_10_insert_rvalue.cpp 33 1
so, know issue relating pointers, don't know how, or should doing here.
in particular, it's line...
dup.push_back(*iter->get());
is there better way set up, or requiring me overload -> operator?
i tried changing (even though prof not want way -- rips current ut files out , puts fresh copies in, wants work above way, , not way)
dup.push_back(*iter.get());
it no longer gives me overloaded errors, giving me now:
error 1 error c2280: 'std::unique_ptr<int,std::default_delete<_ty>>::unique_ptr(const std::unique_ptr<_ty,std::default_delete<_ty>> &)' : attempting reference deleted function
ok, lets @ types here.
you have linkedlist<up>
, std::vector<int>
. when you're trying push element vector using list's iterator, have up
value iterator using iter.get()
, , dereference using operator *
.
so final line should this: dup.push_back(*iter.get());
Comments
Post a Comment