473,246 Members | 1,475 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

Custom template iterator

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
5 3101
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chris Schadl | last post by:
Hi, I've written a simple sorted linked list class that I'm trying to implement an iterator for. I'm trying to make the interface to the iterator simmilar to the STL's iterator interface, so...
1
by: darkstorm | last post by:
Please check this program...When I compiles it in VC.net, it gives the following error: =============== Common\Lib\PList.h(115): error C2440: '=' : cannot convert from 'ListNode *' to...
0
by: nick | last post by:
Hi, I need to manage a "layered" collection of objects, where each layer grows independently, e.g, o--+--+--+--+--+ 1st layer | o 2nd layer (empty) | o--+--+--+ 3rd...
11
by: cyberdave | last post by:
Someone please help me! I have a template class like this: -------------------------------------------------- template<typename T> class List { public:
3
by: chriscorbell | last post by:
I'm curious about what appears to be a restriction on using an STL container inside a user-defined template, esp. using an iterator to such a container. It's not clear to me if this is a general...
2
by: Sherrie Laraurens | last post by:
Hi all, I'm trying to write a generic algorithm routine that will take begin and end iterators of a container, iterate through the range and perform a "calculation" of sorts. The trouble is...
8
by: Fab | last post by:
All, I need your help understanding why the following code does *NOT* compile with G++ (tested with gcc 3.x and 4.1.x): ---------------------------------------------------------------------...
3
by: joe | last post by:
I have written a custom std allocator which follows the example in Stroustrup's book. I'm seeing a behavior I don't understand. My allocate method within the allocator looks like the...
7
by: Max Odendahl | last post by:
Hi, my own declared class has a stl::list as a member variable. I now need access to those from the outside. Is a custom iterator for my class the best solution for this and how to do this? I...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.