Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,999 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,391 people online right now. Registration is fast and FREE... Join Now!




STL iterators & templates

 
Reply to this topicStart new topic

STL iterators & templates

WXY
14 Jan, 2008 - 05:47 AM
Post #1

D.I.C Head
Group Icon

Joined: 2 Jan, 2008
Posts: 85


Dream Kudos: 50
My Contributions
I am still not even remotely literate with C++'s template syntax so this might sound stupid. Anyways I am trying to save time, and from what I've read I've decided this was logical:
CODE

template <typename T> class myClass {
private:
typedef vector<T> tVector;
typedef tVector::iterator tIterator;
..........


It's screwing up on typedef tVector::iterator tIterator; with the message "dependent name is not a type" any ideas why?

This post has been edited by WXY: 14 Jan, 2008 - 05:48 AM
User is offlineProfile CardPM
+Quote Post

Bench
RE: STL Iterators & Templates
14 Jan, 2008 - 08:13 AM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 615



Thanked: 14 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
the problem is that vector<T> is an incomplete type, therefore the compiler does not have enough information on its own to determine whether vector<T>::iterator is a type or something else.

The solution is to provide the compiler with an extra hint that vector<T>::iterator is indeed a type, with the typename keyword

CODE
template <typename T> class myClass {
private:
typedef vector<T> tVector;
typedef typename tVector::iterator tIterator;


The reason the compiler does this, it is entirely possible to create your own specialisation of vector, where vector::iterator is not a type - But since the compiler cannot possibly check every single possible variation on vector<T>::iterator, its up to the you, the programmer to make the assertion that you really did mean to use a type there.

This post has been edited by Bench: 14 Jan, 2008 - 08:21 AM
User is online!Profile CardPM
+Quote Post

WXY
RE: STL Iterators & Templates
14 Jan, 2008 - 05:57 PM
Post #3

D.I.C Head
Group Icon

Joined: 2 Jan, 2008
Posts: 85


Dream Kudos: 50
My Contributions
Thanks, problem solved =)
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 12:12PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month