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

Creating myclass::iterator type?

P: n/a
Hey

I have some code which I've been using on Microsoft VC++ for some time. Now
I wanted to port my application to Mac OS X which offers gcc and the build
fails.

Here is the troublesome code:

template<class T>
class CList
{
public:
typedef vector<T>::iterator iterator;
....
};

As you can see I want to use vector iterator and make it become an iterator
type for my class so I can use CList::iterator.

Gcc however says the following:

.../shared/list.h:47: warning: `std::vector<OLYLXQHQSPOQSPX,
std::allocator<_CharT> >::iterator' is implicitly a typename
.../shared/list.h:47: warning: implicit typename is deprecated, please
see the documentation for details

I wonder why this is deprecated? Does this mean it's not legal C++ ?

Thanks in advance.
-- John
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"John Smith" <jo********@x-formation.com> wrote in message
news:41*********************@dread16.news.tele.dk. ..
Hey

I have some code which I've been using on Microsoft VC++ for some time. Now I wanted to port my application to Mac OS X which offers gcc and the build
fails.

Here is the troublesome code:

template<class T>
class CList
{
public:
typedef vector<T>::iterator iterator;
...
};

As you can see I want to use vector iterator and make it become an iterator type for my class so I can use CList::iterator.

Gcc however says the following:

../shared/list.h:47: warning: `std::vector<OLYLXQHQSPOQSPX,
std::allocator<_CharT> >::iterator' is implicitly a typename
../shared/list.h:47: warning: implicit typename is deprecated, please
see the documentation for details

I wonder why this is deprecated? Does this mean it's not legal C++ ?


Yes, use this instead

typedef typename vector<T>::iterator iterator;

typename is obligatory here, but some compilers do not insist on it.

The reason for requiring typename is that by default C++ assumes iterator is
a static data member. Because vector is a template the compiler cannot look
up the definition of vector<T>::iterator (because the compiler does not know
what T is) so it must be told that iterator is a type name.

john
Jul 22 '05 #2

P: n/a
"John Smith" <jo********@x-formation.com> wrote in message
news:41*********************@dread16.news.tele.dk. ..
Hey

I have some code which I've been using on Microsoft VC++ for some time.
Now
I wanted to port my application to Mac OS X which offers gcc and the build
fails.

Here is the troublesome code:

template<class T>
class CList
{
public:
typedef vector<T>::iterator iterator;
...
};


You're using VC6, right? I just tried your code with VC7.1 (2003) and it
gave the following errors:

cpptest.cpp(9) : warning C4346: 'std::vector<_Ty>::iterator' : dependent
name is not a type
prefix with 'typename' to indicate a type
cpptest.cpp(10) : see reference to class template instantiation 'CList<T>'
being compiled
cpptest.cpp(9) : error C2144: syntax error : 'std::iterator' should be
preceded by ';'
cpptest.cpp(9) : fatal error C1903: unable to recover from previous
error(s); stopping compilation

The point is that when the parser first encounters your reference to
vector<T>::iterator, it doesn't know if it's a type or a static member. VC6
would allow this anyway and just assume it's a type, but that's non-standard
behaviour. VC7 and up, like gcc, don't allow this. As indicated, "prefix
with 'typename' to indicate a type":
typedef typename vector<T>::iterator iterator;

--
Unforgiven

Jul 22 '05 #3

P: n/a
Thanks to both your answers.

To answer forgiven, yes I did use VC6 which works quite ok.

Thank you both for your suggestions. The code works fine now :)

-- John
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.