CAnnot find the compilation error in this circular list

V

Vince

Hi,

I am not used to template and I cannot find what's wrong with this code :

Compiler(MSVS 7) complains at line :
class iterator : public std::iterator<...



Here is the source code

#include <iostream>
#include <list>
#include <string>
using namespace std;

template<class T>
class Ring {
list<T> lst;
public:
// Declaration necessary so the following
// 'friend' statement sees this 'iterator'
// instead of std::iterator:
class iterator;
friend class iterator;
class iterator : public std::iterator<
std::bidirectional_iterator_tag,T,ptrdiff_t>{
list<T>::iterator it;
list<T>* r;
public:
// "typename" necessary to resolve nesting:
iterator(list<T>& lst,
const typename list<T>::iterator& i)
: r(&lst), it(i) {}
bool operator==(const iterator& x) const {
return it == x.it;
}
bool operator!=(const iterator& x) const {
return !(*this == x);
}
list<T>::reference operator*() const {
return *it;
}
iterator& operator++() {
++it;
if(it == r->end())
it = r->begin();
return *this;
}
iterator operator++(int) {
iterator tmp = *this;
++*this;
return tmp;
}
iterator& operator--() {
if(it == r->begin())
it = r->end();
--it;
return *this;
}
iterator operator--(int) {
iterator tmp = *this;
--*this;
return tmp;
}
iterator insert(const T& x){
return iterator(*r, r->insert(it, x));
}
iterator erase() {
return iterator(*r, r->erase(it));
}
};
void push_back(const T& x) {
lst.push_back(x);
}
iterator begin() {
return iterator(lst, lst.begin());
}
int size() { return lst.size(); }
};
 
O

Old Wolf

Vince said:
I am not used to template and I cannot find what's wrong with this code :

Compiler(MSVS 7) complains at line :
class iterator : public std::iterator<...

Here is the source code

using namespace std;

How about not calling your class 'iterator' when there is
already a class called 'iterator' in scope?

Alternatively, drop the "using namespace std;" line. It's
generally a bad idea to have that line in a header file anyway
because it forces everyone who uses your header to also have
std included, and they may not want to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top