Connecting Tech Pros Worldwide Forums | Help | Site Map

can't use typedef of nested class in template class

Frank Bergemann
Guest
 
Posts: n/a
#1: Dec 12 '07
following doesn't work (gcc-3.4.4)
Quote:
cat test2.h
template <class Result>
class MyClass
{
private:
class NestedClass
{
public:
typedef int SubType;
} ;
NestedClass::SubType element;
} ;
Quote:
cat test2.cc
#include "test2.h"
Quote:
/usr/local/gcc-3.4.4/bin/g++ -c test2.cc
In file included from test2.cc:1:
test2.h:12: error: expected `;' before "element"


When i move NestedClass out, it works:

|padsol05 371cat test2.h
class NestedClass
{
public:
typedef int SubType;
} ;

template <class Result>
class MyClass
{
private:
NestedClass::SubType element;
} ;


???

Ian Collins
Guest
 
Posts: n/a
#2: Dec 12 '07

re: can't use typedef of nested class in template class


Frank Bergemann wrote:
Quote:
following doesn't work (gcc-3.4.4)
>
Quote:
>cat test2.h
template <class Result>
class MyClass
{
private:
class NestedClass
{
public:
typedef int SubType;
} ;
NestedClass::SubType element;
You have to tell the compiler NestedClass::SubType is a type:

typename NestedClass::SubType element;

--
Ian Collins.
Jonathan Mcdougall
Guest
 
Posts: n/a
#3: Dec 12 '07

re: can't use typedef of nested class in template class


On Dec 12, 2:27 am, Ian Collins <ian-n...@hotmail.comwrote:
Quote:
Frank Bergemann wrote:
Quote:
following doesn't work (gcc-3.4.4)
>
Quote:
Quote:
cat test2.h
template <class Result>
class MyClass
{
private:
class NestedClass
{
public:
typedef int SubType;
} ;
NestedClass::SubType element;
>
You have to tell the compiler NestedClass::SubType is a type:
>
typename NestedClass::SubType element;
The given example works in Comeau and the FAQ only talks about
nested classes defined in a base class (35.18). As far as I
know, because NestedClass is not a template, there is no need
to do this, although I may be wrong.

--
Jonathan Mcdougall
James Kanze
Guest
 
Posts: n/a
#4: Dec 12 '07

re: can't use typedef of nested class in template class


On Dec 12, 8:27 am, Ian Collins <ian-n...@hotmail.comwrote:
Quote:
Frank Bergemann wrote:
Quote:
following doesn't work (gcc-3.4.4)
Quote:
Quote:
Quote:
cat test2.h
template <class Result>
class MyClass
{
private:
class NestedClass
{
public:
typedef int SubType;
} ;
NestedClass::SubType element;
Quote:
You have to tell the compiler NestedClass::SubType is a type:
Quote:
typename NestedClass::SubType element;
You shouldn't have to. NestedClass is not a dependent type, but
rather a locally defined type, so the compiler knows what is in
it, even without instantiating the template.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Frank Bergemann
Guest
 
Posts: n/a
#5: Dec 12 '07

re: can't use typedef of nested class in template class


- thanks, Ian!
it worked this way.

rgds!

Frank
Ian Collins
Guest
 
Posts: n/a
#6: Dec 13 '07

re: can't use typedef of nested class in template class


James Kanze wrote:
Quote:
On Dec 12, 8:27 am, Ian Collins <ian-n...@hotmail.comwrote:
Quote:
>Frank Bergemann wrote:
Quote:
>>following doesn't work (gcc-3.4.4)
>
Quote:
Quote:
>>>cat test2.h
>>template <class Result>
>>class MyClass
>>{
>> private:
>> class NestedClass
>> {
>> public:
>> typedef int SubType;
>> } ;
>> NestedClass::SubType element;
>
Quote:
>You have to tell the compiler NestedClass::SubType is a type:
>
Quote:
> typename NestedClass::SubType element;
>
You shouldn't have to. NestedClass is not a dependent type, but
rather a locally defined type, so the compiler knows what is in
it, even without instantiating the template.
>
I know, but gcc thinks otherwise!

--
Ian Collins.
Closed Thread