Define an iterator type in a stl container wrapper

Viewed 2804

What it the right way to write a wrapper class around an STL container, which is also a template (can accept a generic type T as element) and allows me to use an iterator as I would do with the STL container directly?

I want to do something of the following type

#include <list>
#include <iostream>

class MyClass{};

template<class T>
class Wrapper
{
    public:
        typename std::list<T>::iterator iterator;

        std::list<T> elements;
        iterator begin(){ return elements.begin(); };
        iterator end(){ return elements.end(); };
};

int main()
{
    Wrapper<MyClass> wrapper;

    for (Wrapper::iterator it = wrapper.begin(); it != wrapper.end(); ++it)
        std::cout<<"Hi"<<std::endl;
}

But the compiler says:

 error: ‘iterator’ in ‘class Wrapper<T>’ does not name a type
2 Answers
Related