wrote:
> On Dec 1, 10:52�pm, "jason.cipri...@gmail.com"
> <jason.cipri...@gmail.com> wrote:
> > I have some code like this that won't compile:
> >
> > template <typename T> class MyClass {
> > public:
> > � � � � class Nested { };
> > � � � � Nested f ();
> >
> > };
> >
> > template <typename T> MyClass<T>::Nested f () { // <---problem
> > � � � � return Nested();
> >
> > }
> >
> > "Nested" is a non-template class, nested inside the template class
> > "MyClass". The MyClass member function f() returns a "Nested", and is
> > defined outside of the class declaration. I receive the following
> > error from Comeau (and similar errors from other compilers):
> >
> > "ComeauTest.c", line 7: error: nontype "MyClass<T>::Nested [with T=T]"
> > is not a
> > � � � � � type name
> > � template <typename T> MyClass<T>::Nested f () {
> >
> > What is the correct syntax to use here? I can't figure out how to make
> > this work (or if it's just not possible). Putting the definition of f
> > () right in the class declaration does work fine, but I'd like to keep
> > it outside, but mostly I'm curious about the syntax.
>
> Oops, I totally screwed that up! Forgot the "MyClass<T>::" before "f
> ()"... I got it figured out. This is correct:
>
> template <typename T> MyClass<T>::Nested MyClass<T>::f () {
> return Nested();
> }
>
> All right, but now that I've fixed *my* stupid mistake, Comeau
> compiles it fine, except MS's VC++ 2008 compiler choked on it:
>
> 1>warning C4346: 'MyClass<T>::Nested' : dependent name is not a type
> 1> prefix with 'typename' to indicate a type
> 1>error C2143: syntax error : missing ';' before 'MyClass<T>::f'
>
> But adding a "typename" in the middle there made it happy for some
> reason:
>
> template <typename T> typename MyClass<T>::Nested MyClass<T>::f () {
> return Nested();
> }
As compiler informs you - MyClass<T>::Nested is dependent name and
should be prefixed with typename keyword
template <typename T> typename MyClass<T>::Nested MyClass<T>::f () {
return Nested();
}
Regards