469,926 Members | 2,497 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

Correct use of templated class'es inner structs/classes?

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).
-----------------------------------

example.h:
template <typename T>
class Y{
public:

struct particle {
OtherClass<T> data;
};

void func();

std::vector<Y<T>::particle> list;
}
example.cpp:
template <typename T>
void Y<T>::func(){
typename std::vector<Y<T>::particle>::iterator i;

i = list.begin();

while(i != list.end()){
// do something
}
}
--------------------------

TIA,
Tomas Ukkonen

Jul 22 '05 #1
4 1350
Tomas Ukkonen wrote in news:pa****************************@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/
Jul 22 '05 #2
Tomas Ukkonen <to***********@iki.fi> wrote in message news:<pa****************************@iki.fi>...
Hi 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.


Indeed. Without knowing T, gcc cannot tell whether particle is
a member type, a data member, a member function, or a member template.

The solution is (obviously) to tell the compiler:

std::vector<typename Y<T>::particle> list;

Regards,
Michiel Salters
Jul 22 '05 #3
On Fri, 07 May 2004 07:04:30 -0700, Michiel Salters wrote:
Tomas Ukkonen <to***********@iki.fi> wrote in message
news:<pa****************************@iki.fi>...
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.


Indeed. Without knowing T, gcc cannot tell whether particle is
a member type, a data member, a member function, or a member template.

The solution is (obviously) to tell the compiler:

std::vector<typename Y<T>::particle> list;


Thanks. I managed to get gcc to accept something similar
while waiting for reply. I have never really read how
templates should work according to standard so I wasn't
sure what the correct behaviour of gcc should be.

--
Tomas Ukkonen
Jul 22 '05 #4
On Fri, 07 May 2004 11:07:49 +0000, Rob Williscroft wrote:
Tomas Ukkonen wrote in news:pa****************************@iki.fi in
comp.lang.c++:
EXAMPLE
(I actually haven't tried to get this compile). Why not ?.


Guess I'm lazy. It was just pseudocode for
illustrating the problem.
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.

Thanks for help. It seems I started asking for help a bit too
quickly as I managed to get the code working myself. I don't have
time to test the latter solution right now.
example.cpp:

<EXAMPLE CODE> This should be in a header file.


Yes, but I usually code in a style where I either put templated code to
cpp files as normally and then include it to header file or
use explicit template instantations.

--
Tomas Ukkonen
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Dan Huantes | last post: by
1 post views Thread by ramunasg | last post: by
4 posts views Thread by jschon | last post: by
12 posts views Thread by Robert.Holic | last post: by
1 post views Thread by cpunerd | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.