473,835 Members | 1,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Custom template iterator

Hi all.

I'm implementing class AbstractCollect ion - 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<typena me T>
class AbstractCollect ion {
public:
AbstractCollect ion() : m_collection() {};
AbstractCollect ion(const AbstractCollect ion &collection) ;
AbstractCollect ion(const int n) : m_collection(n) {};

typedef std::vector<T>: :iterator iterator; //
Try to define iterators for AbstractCollect ion
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 AbstractCollect ion 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 3129
maverik wrote:
Hi all.

I'm implementing class AbstractCollect ion - 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<typena me T>
class AbstractCollect ion {
public:
AbstractCollect ion() : m_collection() {};
AbstractCollect ion(const AbstractCollect ion &collection) ;
AbstractCollect ion(const int n) : m_collection(n) {};

typedef std::vector<T>: :iterator iterator; //
Try to define iterators for AbstractCollect ion
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 AbstractCollect ion 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...@g mail.comwrote:
Hi all.

I'm implementing class AbstractCollect ion - 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<typena me T>
class AbstractCollect ion {
public:
AbstractCollect ion() : m_collection() {};
AbstractCollect ion(const AbstractCollect ion &collection) ;
AbstractCollect ion(const int n) : m_collection(n) {};
AbstractCollect ion( 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.be gin(); }

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 AbstractCollect ion 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 AbstractCollect ion 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 AbstractCollect ion 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 AbstractCollect ion 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.com wrote:
maverik wrote:
Nothing wrong in encapsulating a std::vector,
you are doing it exactly as planned.
The only issue is the name AbstractCollect ion 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 objektorientier ter Datenverarbeitu ng
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
8710
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 one could do: SortedList<int> sl; SortedList<int>::iterator i; for (i = sl.begin() ; i != sl.end() ; ++) { /* ... */ }
1
2130
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 'List<T>::ListNode *' with Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast project\Source\Levelmgr.cpp(198) : while compiling
0
1951
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 layer |
11
3614
by: cyberdave | last post by:
Someone please help me! I have a template class like this: -------------------------------------------------- template<typename T> class List { public:
3
2402
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 template/language restriction, and STL iterator limitation, or if I'm just going about it wrong. I'm declaring a template which uses a std::map to store references to the template type, e.g. template template <typename T>
2
2151
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 that the algorithm will behave differently for the two different types. I've considered calling the algorithm foo_A and foo_B, but I don't like that approach because it will blow out in naming complexity down the track.
8
1887
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): --------------------------------------------------------------------- template<class T> class myvector { // Just a simplification of std::vector class iterator { T a; iterator &operator++(); };
3
1904
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 following: (FooAllocator<T>) pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer hint = 0)
7
3840
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 would like to sort the list according to different criteria before, so I would like to offer different iterators, which then sort first based on the type of iterator constructed. Any code sample or link would be nice.
0
9652
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10811
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10233
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9345
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7766
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5804
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4434
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3088
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.