Creating myclass::iterator type?

J

John Smith

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
 
U

Unforgiven

John Smith said:
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;
 
J

John Harrison

John Smith said:
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
 
J

John Smith

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,235
Latest member
Top Crypto Podcasts_

Latest Threads

Top