Hi,
I am trying to implement an iterator to a List class. The code is included below. I get the following compiler error. At the point where I use a class that is defined inside a template (nList<T>::Link ). The Link class is defined inside of a template. (I assume that is possible?). Any one know what I doing wrong?
Many Thanks,
Purush
Error from Compiler (gcc 3.4.6)
nList.cc:52: error: expected `;' before "current_"
*** Error code 1
Stop in /usr/home/annu/work/c++/Containers/dlists.
I am trying to implement an iterator to a List class. The code is included below. I get the following compiler error. At the point where I use a class that is defined inside a template (nList<T>::Link ). The Link class is defined inside of a template. (I assume that is possible?). Any one know what I doing wrong?
Many Thanks,
Purush
Error from Compiler (gcc 3.4.6)
nList.cc:52: error: expected `;' before "current_"
*** Error code 1
Stop in /usr/home/annu/work/c++/Containers/dlists.
Code:
#include <iostream>
template<class T> class Iterator
{
public:
virtual T* first() = 0;
virtual T* next() = 0;
virtual T* previous() = 0;
};
template<class T> class List_Iterator;
template<class T> class nList
{
public:
nList(): head_(0), tail_(0), size_(0) {}
void push_front(const T& r);
void push_back(const T& r);
void insert(int position, const T r);
int size() const;
void print() const;
class Bad_Range{};
//friend class List_Iterator<T>;
class Link
{
public:
Link* next;
Link* previous;
T val;
Link(T v):next(0), previous(0),val(v) {}
Link():next(0), previous(0) {}
};
private:
Link* head_;
Link* tail_;
int size_;
};
template<class T> class List_Iterator : public Iterator<T>
{
public:
T* first();
T* next();
T* previous();
List_Iterator(nList<T>& l): list_(l){};
private:
nList<T>& list_;
nList<T>::Link current_;
};
//template<class T> T* List_Iterator<T>::first()
//{
// return &list_.front();
//}
//
//template<class T> T* List_Iterator<T>::next()
//{
// if (current_)
// current_ = current_->next;
//
// return ¤t_->val;
//}
//template<class T> T* List_Iterator<T>::previous()
//{
// if (current_)
// current_ = current_->previous;
//
// return ¤t_->val;
//}
template<class T> int nList<T>::size() const
{
return size_;
}
template<class T> void nList<T>::print() const
{
Link* l;
for (l = head_; l; l = l->next)
std::cout<<l->val<<'\n';
}
template<class T> void nList<T>::push_front(const T& r)
{
Link* l = new Link(r);
l->next = head_;
l->previous = 0;
if (head_)
head_->previous = l;
head_ = l;
if (!tail_)
tail_ = l;
size_++;
}
template<class T> void nList<T>::push_back(const T& r)
{
Link* l = new Link(r);
l->previous = tail_;
l->next = 0;
if (tail_)
tail_->next = l;
tail_ = l;
if (!head_)
head_ = l;
size_++;
}
template<class T> void nList<T>::insert(int position, const T r)
{
if (position > size_)
throw Bad_Range();
Link* l = new Link(r);
Link* iterator = head_;
for (int i = 0; i < position; i++)
iterator = iterator->next ;
if (iterator)
{
iterator->previous->next = l;
l->next = iterator;
iterator->previous = l;
}
else
{
push_back(r);
}
size_++;
}
int main()
{
nList<int> il;
}
Comment