templates - C++ Custom List Iterator Errors -
i have started writing own library common data structures, educational purposes (ie: personal practice). however, encountered few problems cannot seem resolve myself on singly linked list. not c++ expert , code may have problems, please don't harsh on me :)
i have tried implement list_node class declares , defines node list, list_iterator class acts forward iterator singly linked list requires, list class supposed give make use of mentioned classes , provide functionality list.
i attach code , errors under visual studio.
list_node.h
/** * list_node.h * * @author raul butuc. * @version 1.0.0 16/03/2015 */ #pragma once #include "list.h" #include "list_iterator.h" namespace my_library { template <class _tp> class list_node { friend class list<_tp>; friend class list_iterator<_tp>; private: _tp m_node; list_node<_tp> *m_pnext; list_node(const _tp&, list_node<_tp>*); ~list_node(); }; template <class _tp> list_node<_tp>::list_node(const _tp& node, list_node<_tp>* next) : m_node(node), m_pnext(next) {} template <class _tp> list_node<_tp>::~list_node() { delete m_pnext; } } list_iterator.h
/** * list_iterator.h * * @author raul butuc. * @version 1.0.0 16/03/2015 */ #pragma once #include <cassert> #include <iterator> #include "list.h" #include "list_node.h" namespace my_library { template <class _tp> class list; template <class _tp> class list_node; template <class _tp> class list_iterator { public: // line below has error c2059: syntax error : '<' // error c2238: unexpected token(s) preceding ';' friend class list<_tp>; typedef list_iterator<_tp> iterator; typedef size_t size_type; typedef _tp value_type; typedef _tp& reference; typedef _tp* pointer; typedef std::forward_iterator_tag iterator_category; typedef int difference_type; const iterator& operator++(); const iterator& operator++(int); reference operator*(); pointer operator->(); bool operator==(const iterator&) const; bool operator!=(const iterator&) const; private: list_node<_tp>* m_pnode; list_iterator(list_node<_tp>*); }; template <class _tp> list_iterator<_tp>::list_iterator(list_node<_tp>* pnode) : m_pnode(pnode) {} template <class _tp> const list_iterator<_tp>::iterator& list_iterator<_tp>::operator++() { assert(m_pnode != null); m_pnode = m_pnode->m_pnext; return *this; } template <class _tp> const list_iterator<_tp>::iterator& list_iterator<_tp>::operator++(int) { list_iterator<_tp>::iterator _tmp = *this; ++(*this); return _tmp; } template <class _tp> list_iterator<_tp>::reference list_iterator<_tp>::operator*() { return m_pnode->m_node; } template <class _tp> list_iterator<_tp>::pointer list_iterator<_tp>::operator->() { return m_pnode; } template <class _tp> bool list_iterator<_tp>::operator==(const list_iterator<_tp>::iterator& other) const { return m_pnode == other->m_pnode; } template <class _tp> bool list_iterator<_tp>::operator!=(const list_iterator<_tp>::iterator& other) const { return m_pnode != other->m_pnode; } } list.h
/** * list.h * * @author raul butuc. * @version 1.0.0 16/03/2015 */ #pragma once #include "list_node.h" #include "list_iterator.h" namespace my_library { template <class _tp> class list { public: typedef list_iterator<_tp> iterator; list(); ~list(); bool empty() const; void push_back(const _tp&); iterator begin(); iterator end(); private: list_node<_tp>* m_phead; list_node<_tp>* m_ptail; }; template <class _tp> list<_tp>::list() : m_phead(null), m_ptail(null) {} template <class _tp> list<_tp>::~list() { delete m_phead; } template <class _tp> bool list<_tp>::empty() const { return m_phead == null; } template <class _tp> void list<_tp>::push_back(const _tp& node) { list_node<_tp>* _node = new list_node<_tp>(node, null); if (m_phead == null) { m_phead = _node; } else { m_ptail->m_pnext = _node; } m_ptail = _node; } template <class _tp> list<_tp>::iterator list<_tp>::begin() { return list_iterator<_tp>(m_phead); } template <class _tp> list<_tp>::iterator list<_tp>::end() { return list_iterator<_tp>(null); } } test.cpp
/** * test.cpp * * @author raul butuc. * @version 1.0.0 16/03/2015 */ #include <iostream> #include "list.h" using std::cout; using namespace my_library; int main(int argc, char* argv[]) { list<int> list; if (list.empty()) { std::cout << "list empty" << "\n"; } (int = 0; < 100; += 10) { list.push_back(i); } // here should allow me write like: // list<int>::iterator = list.begin(); // however, not. please explain // why so? sorry if due naive mistake // trying learn. again :) list_iterator<int> = list.begin(); (; != list.end(); ++it) { std::cout << *it << " "; } std::cout << "\n"; system("pause"); return 0; } errors @ compilation
1>------ build started: project: list, configuration: debug win32 ------
1> test.cpp
1>d:...\list_iterator.h(23): error c2059: syntax error : '<'
1>\d:...\list_iterator.h(43) : see reference class template instantiation 'my_library::list_iterator<_tp>' being compiled
1>d:...\list_iterator.h(23): error c2238: unexpected token(s) preceding ';'
1>d:...\list_iterator.h(40): error c2143: syntax error : missing ';' before '<'
1>d:...\list_iterator.h(40): error c4430: missing type specifier - int assumed. note: c++ not support default-int
1>d:...\list_iterator.h(40): error c2238: unexpected token(s) preceding ';'
1>d:...\list_iterator.h(41): error c2061: syntax error : identifier 'list_node'
1>d:...\list_iterator.h(46): error c2061: syntax error : identifier 'list_node'
1>d:...\list_iterator.h(49): warning c4346: 'my_library::list_iterator<_tp>::iterator' : dependent name not type
1> prefix 'typename' indicate type
1>d:...\list_iterator.h(49): error c2143: syntax error : missing ';' before '&'
1>d:...\list_iterator.h(49): error c2065: '_tp' : undeclared identifier
1>d:...\list_iterator.h(49): error c2923: 'my_library::list_iterator' : '_tp' not valid template type argument parameter '_tp'
1>d:...\list_iterator.h(53): error c2509: '++' : member function not declared in 'my_library::list_iterator'
1>
d:...\list_iterator.h(18) : see declaration of 'my_library::list_iterator'
1>d:...\list_iterator.h(53): fatal error c1903: unable recover previous error(s); stopping compilation
========== build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
thank taking time!
edit:
thanks mebob, indeed main problem. however, after fixing that, errors on line 52 , 56 in list_iterator.h:
1>d:\programming - data structures\list\list\list_iterator.h(52): warning c4346: 'my_library::list_iterator::iterator' : dependent name not type
1> prefix 'typename' indicate type
1>d:\programming - data structures\list\list\list_iterator.h(52): error c2143: syntax error : missing ';' before '&'
1>d:\programming - data structures\list\list\list_iterator.h(52): error c2065: 't' : undeclared identifier
1>d:\programming - data structures\list\list\list_iterator.h(52): error c2923: 'my_library::list_iterator' : 't' not valid template type argument parameter 't'
1>d:\programming - data structures\list\list\list_iterator.h(56): error c2509: '++' : member function not declared in 'my_library::list_iterator'
1>
d:\programming - data structures\list\list\list_iterator.h(21) : see declaration of 'my_library::list_iterator'
1>d:\programming - data structures\list\list\list_iterator.h(56): fatal error c1903: unable recover previous error(s); stopping compilation
i changed _tp t in classes (in code, didn't update here)
edit 2:
even after getting on errors, seems won't compile, time because of operators.
the code directly involved is:
// other code... template <class _tp> typename list_iterator<_tp>::pointer list_iterator<_tp>::operator->() { return m_pnode; } template <class _tp> bool list_iterator<_tp>::operator==(const typename list_iterator<_tp>::iterator& other) const { /* error c2678: binary '->' : no operator found takes left-hand operand of type 'const my_library::list_iterator<_tp>' (or there no acceptable conversion) */ return m_pnode == other->m_pnode; } // other code... from list_iterator.h
you must have forward declarations, so:
template <class _tp> class list; template <class _tp> class list_iterator; before friend declarations because otherwise, when compiler sees friend class list doesn't know expect template argument list.
as other error, must use const typename list_iterator<_tp>::iterator& type. time refer type that's member of templated class (or struct) must use typename keyword beforehand clarify compiler expecting type.
your last error because attempting call non-const operator->() on const. fix create const version of returns const pointer.
also, neil kirk commented, _tp reserved identifier shouldn't using that.
Comments
Post a Comment