I'm getting the following error from g++ while trying to add iterator support for my linked list class.
LinkedList.hpp: In member function ‘Type& exscape::LinkedList<Type>::iterator::operator*() [with Type = int]’: tests.cpp:51: instantiated from here LinkedList.hpp:412: error: invalid initialization of non-const reference of type ‘int&’ from a temporary of type ‘exscape::LinkedList<int>::iterator*’
Likely relevant code snippets:
LinkedList.hpp:
template <typename Type> class LinkedList { private: struct node { struct node *prev; struct node *next; Type data; }; public: class iterator : public std::iterator<...> { node *p; public: Type &operator*(); }; ... }; template <typename Type> LinkedList<Type>::iterator::iterator(struct node *in_node) : p(in_node) {} template <typename Type> inline Type &LinkedList<Type>::iterator::operator*() { return this-p->data; ///// Line 412 } tests.cpp:
... LinkedList<int> l1; ... LinkedList<int>::iterator it; for (it = l1.begin(); it != l1.end(); ++it) { std::cout << "Element: " << *it << std::endl; ///// Line 51 } I've googled (and searched SO, of course), and checked my code to no avail - either I'm missing something basic (aka. doing something stupid), or I'm missing the knowledge required. Any advice?