Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C++ Programming

Custom iterator for STL-style container


The container is:

template <typename T>
class Container {
public:
        // container methods..

        // iterator:
        class const_iterator {
        public:
                const_iterator(T* i)
                        : pntr(i) { }

                const_iterator& operator=
(const_iterator);
                const_iterator& operator++();
                const_iterator  operator++(const_iterator);
                const_iterator& operator--();
                const_iterator  operator--(const_iterator);
                bool            operator==(const_iterator);
                bool            operator!=(const_iterator);

        private:
                T* pntr;
        };

        const_iterator begin() const;
        const_iterator end() const;

private:
        std::vector<T> v;

};

// Well... implementation for Container<T>::begin() :
template <typename T>
Container<T>::const_iterator Container<T>::begin() const
{
        const_iterator i(&v[0]);
        return i;

}

g++ says: "error: expected constructor, destructor, or type conversion
before 'Container'"
Where is it wrong? Which is the right way to do that?

Thanks in advance,

s.

"gallows" ,comp.lang.c++:

> template <typename T>
> Container<T>::const_iterator Container<T>::begin() const

Container<T>::const_iterator is a qualified (by Container<T>) and
dependent (on T) name; therefore, you have to prefix it by the keyword
"typename" to disambiguate the fact that it is the name of a type
(Container<T>::const_iterator could very well be the name of a data
member for some T).
On Mar 5, 10:49 am, "gallows" <g4ll@gmail.com> wrote:

This should be:

 const_iterator  operator++(int);

>                 const_iterator& operator--();
>                 const_iterator  operator--(const_iterator);

This should be:

 const_iterator  operator--(int);

Add "typename" (see http://womble.decadentplace.org.uk/c++/template-faq.html#type-syntax-...

 typename Container<T>::const_iterator Container<T>::begin() const

> {
>         const_iterator i(&v[0]);
>         return i;

> }

> g++ says: "error: expected constructor, destructor, or type conversion
> before 'Container'"
> Where is it wrong? Which is the right way to do that?

Cheers! --M
Clear, I Understood. Thanks a lot!
Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc