I'm working on making a linkedlist class for some practice. I want it to support iteration, so I've given it an iterator class. However, I'm getting some compilation errors. Here is a shortened version of my code.
t.cpp
#include "t.hpp"
int main()
{
return 0;
}
t.hpp
#ifndef LINKEDLIST_HPP
#define LINKEDLIST_HPP 1
#include <cstddef>
#include <iterator>
template <typename T>
class LinkedList {
private:
template <typename V> class Iterator;
template <typename V> struct ListNode;
public:
typedef Iterator<T> iterator;
iterator begin();
iterator end();
private:
template <typename V>
class Iterator : public std::iterator<std::forward_iterator_tag, T> {
public:
Iterator<T>& operator++();
private:
friend class LinkedList;
Iterator() = delete;
Iterator(ListNode<T>* node) : current_{node} { }
ListNode<T>* current_;
};
template <typename V>
struct ListNode
{
T value_;
ListNode<T>* next_;
};
ListNode<T>* head_;
ListNode<T>* tail_;
};
template <typename T> inline
typename LinkedList<T>::iterator LinkedList<T>::begin()
{
return iterator{head_};
}
template <typename T> inline
typename LinkedList<T>::iterator LinkedList<T>::end()
{
return iterator{nullptr};
}
template <typename T> inline
typename LinkedList<T>::iterator&
LinkedList<T>::iterator::operator++()
{
current_ = current_->next_;
return *this;
}
#endif
Now I have a sneaking suspicion that the line typedef Iterator<T> iterator
either doesn't quite work like I think it should, or it is bad practice by concealing the fact that iterator
is a template type. However, I don't get any obvious warnings from my compiler (mingw32-g++ 4.8.1) about that.
I'm compiling this using
$ mingw32-g++ -g -Wall -Werror -Wextra -pedantic -std=gnu++11 t.cpp
which gives me the error
t.hpp:60:33: error: specializing member 'LinkedList<T>::Iterator<T>::operator++' requires 'template<>' syntax
typename LinkedList<T>::iterator&
^
Okay, so I give it the template syntax there.
template <typename T> inline
typename LinkedList<T>::iterator<T>&
LinkedList<T>::iterator::operator++() { }
Where I then get the compilation error
t.hpp:60:25: error: non-template 'iterator' used as template
typename LinkedList<T>::iterator<T>&
^
t.hpp:60:25: note: use 'LinkedList<T>::template iterator' to indicate that it is a template
t.hpp:60:25: error: expected unqualified-id at end of input
^
Okay, so I use template
template <typename T> inline
typename LinkedList<T>::template iterator<T>&
LinkedList<T>::iterator::operator++() { }
And get this error now
t.hpp:60:45: error: specializing member 'LinkedList<T>::Iterator<T>::operator++' requires 'template<>' syntax
typename LinkedList<T>::template iterator<T>&
^
Which is where I get stuck. I've read these questions
- specializing member S::display requires ‘template<>’ syntax
- Why does this code give the error, "template specialization requires 'template<>'"?
- C++: specializing member requires «template<>» syntax
which seem to indicate that I need to put a template<>
somewhere. I'm just not sure I understand where it should go.
How should I attempt to do what I'm doing here?