This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Template declaration problem?
- To: <egcs-bugs at cygnus dot com>
- Subject: Template declaration problem?
- From: "Bruce Eckel" <Eckel at CrestedButte dot net>
- Date: Tue, 15 Sep 1998 09:28:58 -0600
- Reply-To: "Bruce Eckel" <Eckel at CrestedButte dot net>
This compiles and runs with KCC, but has some interesting problems compiling
with egcs 1.1:
//: C20:Ring.cpp
// Making a "ring" data structure from the STL
#include <iostream>
#include <list>
#include <string>
using namespace std;
template<class T>
class Ring {
list<T> r;
friend class iterator;
public:
class iterator : public list<T>::iterator {
list<T> r;
public:
iterator(const list<T>& lst, const list<T>::iterator& i)
: r(lst), list<T>::iterator(i) {}
bool operator==(const iterator& x) const
return node == x.node;
}
bool operator!=(const iterator& x) const
return !(*this == x);
}
list<T>::reference operator*() const
return (*node).data;
}
iterator& operator++() {
if(*this == iterator(r, r.end()))
*this = iterator(r, r.begin());
else
node = (list<T>::link_type)((*node).next);
return *this;
}
iterator operator++(int) {
iterator tmp = *this;
++*this;
return tmp;
}
iterator& operator--() {
if(*this == iterator(r, r.begin()))
*this = iterator(r, r.end());
node = (list<T>::link_type)((*node).prev);
return *this;
}
iterator operator--(int) {
iterator tmp = *this;
--*this;
return tmp;
}
};
void push_back(const T& x) {
r.push_back(x);
}
iterator begin()
return iterator(r, r.begin());
}
iterator end()
return iterator(r, r.end());
}
iterator insert (iterator position,const T& x){
return iterator(r.insert(position, x));
}
iterator erase (iterator position) {
return iterator(r.erase(position));
}
};
int main() {
Ring<string> rs;
rs.push_back("one");
rs.push_back("two");
rs.push_back("three");
rs.push_back("four");
rs.push_back("five");
copy(rs.begin(), rs.end(), ostream_iterator<string>(cout, ", "));
} ///:~
================================
Bruce Eckel http://www.BruceEckel.com
Contains free electronic books: "Thinking in Java" and "Thinking in C++ 2e"