[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Correct use of templated class'es inner structs/classes?
From: |
Rob Williscroft |
Subject: |
Re: Correct use of templated class'es inner structs/classes? |
Date: |
7 May 2004 11:07:49 GMT |
User-agent: |
Xnews/5.04.25 |
Tomas Ukkonen wrote in pan.2004.05.07.09.54.54.797578@iki.fi">news:pan.2004.05.07.09.54.54.797578@iki.fi in
comp.lang.c++:
> Hi
>
> I'm currently trying to make my old code to compile with
> new gcc 3.4.0. It compiles correctly in 3.3.3 so I'm not
> sure if this is compiler bug or not.
>
>
> Code very close to a example below fails to compile
> because gcc 3.4.0 complains:
> "error: expected a type got 'Y<T>::particle'"
> at Y<T>::func()'s iterator declaration.
>
>
> EXAMPLE
> (I actually haven't tried to get this compile).
Why not ?.
> -----------------------------------
>
> example.h:
> template <typename T>
> class Y{
> public:
>
> struct particle {
> OtherClass<T> data;
T data; // to make a compilable example !
> };
>
> void func();
>
> std::vector<Y<T>::particle> list;
std::vector< typename Y<T>::particle > list;
However Y (without the <T>) is an injected class name so you should
be able to do this:
std::vector< Y::particle > list;
However of the compilers I tried on CBuilderX previeiw (EDG based)
handled it, g++ 3.4 (prereleas) didn't, your version maybe later than
mine so try it.
> }
missing ;
>
>
> example.cpp:
This should be in a header file.
> template <typename T>
> void Y<T>::func(){
> typename std::vector< Y<T>::particle >::iterator i;
typename std::vector< typename Y<T>::particle >::iterator i;
>
> i = list.begin();
>
> while(i != list.end()){
> // do something
> }
> }
missing ;
The typename is required as Y<T> is dependant on a template argument
so particle in Y<T>::particle is a dependant-name, you need to tell
the compiler its a type, otherwise it assumes its a non-type.
g++ 3.3.x didn't require this as it didn't support 2-phase name
lookup, as its supposed to. i.e. 3.4 is correct.
HTH.
Rob.
--
http://www.victim-prime.dsl.pipex.com/