Template declaration problem?
Bruce Eckel
Eckel@CrestedButte.net
Wed Sep 16 13:51:00 GMT 1998
I tried a number of different attempts at forward declaration, and all were
rejected by 1.1. Can you give me a hint?
In the meantime, I've cleaned up the program significantly. It now compiles
and runs both with KCC and VC++ (latest). Here it is:
//: 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> lst;
friend class iterator;
public:
class iterator: public std::iterator<
std::bidirectional_iterator_tag,T,ptrdiff_t>{
list<T>::iterator it;
list<T>* r;
public:
iterator(list<T>& lst,
const 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(); }
};
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");
Ring<string>::iterator it = rs.begin();
it++; it++;
it.insert("six");
it = rs.begin();
// Twice around the ring:
for(int i = 0; i < rs.size() * 2; i++)
cout << *it++ << endl;
} ///:~
================================
Bruce Eckel http://www.BruceEckel.com
Contains free electronic books: "Thinking in Java" and "Thinking in C++ 2e"
-----Original Message-----
From: Alexandre Oliva <oliva@dcc.unicamp.br>
To: Bruce Eckel <Eckel@CrestedButte.net>
Cc: egcs-bugs@cygnus.com <egcs-bugs@cygnus.com>; egcs-patches@cygnus.com
<egcs-patches@cygnus.com>; jason@cygnus.com <jason@cygnus.com>
Date: Tuesday, September 15, 1998 5:04 PM
Subject: Re: Template declaration problem?
>Bruce Eckel <Eckel@CrestedButte.net> writes:
>
>> This compiles and runs with KCC, but has some interesting problems
>> compiling with egcs 1.1:
>
>In fact, there are a few bugs in your code that prevent egcs from
>compiling it correctly. First, when you declare:
>
>> template<class T> class Ring {
>> friend class iterator;
>
>Unless Ring<T>::iterator is already declared, this declaration refers
>to a class `iterator' in the outer namespace scope, not a nested
>class. So you have to insert a forward declaration of nested class
>`iterator' before the friend declaration.
>
>The lack of this declaration causes a name lookup problem in egcs, but
>class Ring::iterator won't be granted friendship with the declaration
>as it is.
>
>> class iterator : public list<T>::iterator {
>[snip]
>> return node == x.node;
>
>Note that `node' is not a standardized member of list<T>::iterator;
>although it works with the STL distributed with egcs 1.1, it does not
>work with the one in the latest snapshot of egcs!
>
>> iterator insert (iterator position,const T& x){
>> return iterator(r.insert(position, x));
>> }
>
>The only constructor of class Ring<T>::iterator takes more than one
>argument; isn't the first argument missing? Same problem in method
>erase.
>
>
>The name lookup problem I refer to only affects friend declarations
>that introduce new names in outer namespaces; if the class iterator
>had been forward-declared outside the template definition, the correct
>class name would be looked up in the member functions of Ring<T>.
>
>Jason, Mark, any idea about how to fix this problem? May I install
>a preprocessed version of the attached test case in g++.pt?
>
>--
>Alexandre Oliva
> mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
> http://www.dcc.unicamp.br/~oliva
>Universidade Estadual de Campinas, SP, Brasil
>
More information about the Gcc-bugs
mailing list