Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Custom iterator for STL-style container

Reply
Thread Tools

Custom iterator for STL-style container

 
 
gallows
Guest
Posts: n/a
 
      03-05-2007
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.

 
Reply With Quote
 
 
 
 
Pierre Senellart
Guest
Posts: n/a
 
      03-05-2007
"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).
 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      03-05-2007
On Mar 5, 10:49 am, "gallows" <(E-Mail Removed)> wrote:
> 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);


This should be:

const_iterator operator++(int);

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


This should be:

const_iterator operator--(int);

> 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


Add "typename" (see http://womble.decadentplace.org.uk/c...syntax-error):

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

 
Reply With Quote
 
gallows
Guest
Posts: n/a
 
      03-05-2007
Clear, I Understood. Thanks a lot!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Custom bidimensional iterator and container nick C++ 0 09-10-2005 11:25 PM
std::container::iterator vs std::container::pointer Vivi Orunitia C++ 11 02-04-2004 08:09 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments