By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,465 Members | 1,600 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,465 IT Pros & Developers. It's quick & easy.

Custom template iterator

P: n/a
Hi all.

I'm implementing class AbstractCollection - just a wrap on std::vector
- to hide from clients the fact of using std::vector in the base
(because in the future I may change vector to a list or another DS).
So I try to do it in this way:

template<typename T>
class AbstractCollection {
public:
AbstractCollection() : m_collection() {};
AbstractCollection(const AbstractCollection &collection);
AbstractCollection(const int n) : m_collection(n) {};

typedef std::vector<T>::iterator iterator; //
Try to define iterators for AbstractCollection
typedef std::vector<T>::const_iterator const_iterator;
private:
std::vector<Tm_collection;
};

But the complier complains:

error C2146: syntax error : missing ';' before identifier 'iterator'
....

Can you tell me how to do it in the right way (how can I define
iterators for AbstractCollection using std::vector iterators)?
It would be nice if you also tell me what's wrong with my method (or
provide link to read about).

Thanks.
Nov 7 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
maverik wrote:
Hi all.

I'm implementing class AbstractCollection - just a wrap on std::vector
- to hide from clients the fact of using std::vector in the base
(because in the future I may change vector to a list or another DS).
So I try to do it in this way:

template<typename T>
class AbstractCollection {
public:
AbstractCollection() : m_collection() {};
AbstractCollection(const AbstractCollection &collection);
AbstractCollection(const int n) : m_collection(n) {};

typedef std::vector<T>::iterator iterator; //
Try to define iterators for AbstractCollection
typedef std::vector<T>::const_iterator const_iterator;
The correct way is:
typedef typename std::vector<T>::const_iterator const_iterator;
private:
std::vector<Tm_collection;
};

But the complier complains:

error C2146: syntax error : missing ';' before identifier 'iterator'
...

Can you tell me how to do it in the right way (how can I define
iterators for AbstractCollection using std::vector iterators)?
It would be nice if you also tell me what's wrong with my method (or
provide link to read about).
Search for "template dependent name"
Nov 7 '08 #2

P: n/a
On Nov 7, 6:42 am, maverik <maverik.m...@gmail.comwrote:
Hi all.

I'm implementing class AbstractCollection - just a wrap on std::vector
- to hide from clients the fact of using std::vector in the base
(because in the future I may change vector to a list or another DS).
Exactly, that should give you a clue why you get the error.
A list and a vector do not have the same type of iterator.
So I try to do it in this way:

template<typename T>
class AbstractCollection {
public:
AbstractCollection() : m_collection() {};
AbstractCollection(const AbstractCollection &collection);
AbstractCollection(const int n) : m_collection(n) {};
AbstractCollection( const std::size_t n, const T& t)
: m_collection(n, t) {};
>
typedef std::vector<T>::iterator iterator;
Since std::vector<T>::iterator is a dependent type:

typedef typename std::vector<T>::iterator iterator;

iterator begin() { return m_collection.begin(); }

and so on...
typedef std::vector<T>::const_iterator const_iterator;
private:
std::vector<Tm_collection;

};

But the complier complains:

error C2146: syntax error : missing ';' before identifier 'iterator'
...

Can you tell me how to do it in the right way (how can I define
iterators for AbstractCollection using std::vector iterators)?
It would be nice if you also tell me what's wrong with my method (or
provide link to read about).
Nothing wrong in encapsulating a std::vector,
you are doing it exactly as planned.
The only issue is the name AbstractCollection since its not abstract?
How about Vector, Container or Collection.

Nov 7 '08 #3

P: n/a
Thank you guys.
Nothing wrong in encapsulating a std::vector,
you are doing it exactly as planned.
The only issue is the name AbstractCollection since its not abstract?
How about Vector, Container or Collection.
Thanks, you are right.
Nov 8 '08 #4

P: n/a
maverik wrote:
Thank you guys.
>Nothing wrong in encapsulating a std::vector,
you are doing it exactly as planned.
The only issue is the name AbstractCollection since its not abstract?
How about Vector, Container or Collection.

Thanks, you are right.
That's not the only issue. Check Item 2 in "Effective STL" by
Scott Meyers:

Beware the illusion of container-independent code

--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
Nov 8 '08 #5

P: n/a
On Nov 8, 2:17*pm, Gennaro Prota <gennaro/pr...@yahoo.comwrote:
maverik wrote:
Nothing wrong in encapsulating a std::vector,
you are doing it exactly as planned.
The only issue is the name AbstractCollection since its not abstract?
How about Vector, Container or Collection.
Thanks, you are right.
That's not the only issue. Check Item 2 in "Effective STL" by
Scott Meyers:
* *Beware the illusion of container-independent code
Exactly. As long as the iterator is just a typedef, he's not
encapsulating anything. To effectively encapsulate, he also has
to encapsulate the iterator.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 9 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.