View Single Post
Old 12-21-2007, 02:49 AM   #1 (permalink)
messenjah
Recruit
 
Join Date: Dec 2007
Posts: 2
messenjah is on a distinguished road
Iterator definition outside class

I am trying to implement a graph class which allows neighbour access via an iterator. I have the following so far.
Code:
template <typename T>
class Graph {
    public:
        class iterator;
        friend class iterator;
        // Lots of constructors, destructor and other functions
        iterator begin(){
            iterator it;
            it.index = 0;
            it.container = this;
            return it;
        }
        iterator end();
        // Other functions, protected and private data
};

template <typename T>
class Graph<T>::iterator {
    friend class Graph<T>;
    public:
        // Lots of constructors, destructor and other functions
    protected:
        int index;
        Graph<T>* container;
};

// Some other code

template <typename T>
Graph<T>::iterator Graph<T>::end(){ // Error Line
    iterator it;
    it.index = this->maxsize;
    it.container = this;
    return it;
}
The point I want to make here is that the iterator function begin is implemented in the actual Graph class declaration while the other one end is outside the class. When I try to precompile the header I get an error expected constructor, destructor, or type conversion before "Graph" at the indicated line (Error Line). When I define both iterator functions inside the class evrything compiles OK. Ideally I would like both iterator functins outside the class. Any ideas anyone. btw I am using g++ 3.4.4-2 on RH9 Enterprise.
messenjah is offline   Reply With Quote