Reputation:
My overloaded prefix operator is having issues finding the scope.
#ifndef LIST_H
#define LIST_H
#include <iostream>
using namespace std;
template <typename T>
struct node
{
T data;
node* next;
node* prev;
};
template <typename T>
class list
{
public:
class iterator
{
public:
iterator() : inode(0) {}
iterator(node<T>* current) : inode(current) {}
// overload prefix increment operator (++x)
iterator& operator ++();
}
private:
node<T>* inode; //
};
iterator begin();
iterator end();
private:
// pointer to the head of the dclist
node<T>* head;
};
// constructor
template <typename T>
list<T>::list() {
head = new node<T>;
head->next = head;
head->prev = head;
}
template <typename T> // This function needs proper scope resolution
typename list<T>::iterator::iterator& iterator<T>::operator ++() {
inode = inode->next;
return this;
}
// return iterate from beginning //
template <typename T>
typename list<T>::iterator list<T>::begin() {
return list<T>::iterator(head->next);
}
// return iterate to end //
template <typename T>
typename list<T>::iterator list<T>::end() {
return list<T>::iterator(head);
}
#endif
Upvotes: 1
Views: 1232
Reputation: 355207
The function name is not properly qualified and the return type has an extra iterator
:
template <typename T>
typename list<T>::iterator::iterator& iterator<T>::operator ++()
This needs to be:
template <typename T>
typename list<T>::iterator& list<T>::iterator::operator++()
Note also that using using namespace std;
and declaring your own types with names identical to Standard Library names (list
, iterator
, etc.) can only end in tears. It would behoove you to drop the using namespace std;
.
Upvotes: 3