can't use typedef of nested class in template class

F

Frank Bergemann

following doesn't work (gcc-3.4.4)
cat test2.h
template <class Result>
class MyClass
{
private:
class NestedClass
{
public:
typedef int SubType;
} ;
NestedClass::SubType element;
} ;
cat test2.cc
#include "test2.h"
/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 371> cat test2.h
class NestedClass
{
public:
typedef int SubType;
} ;

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


???
 
I

Ian Collins

Frank said:
following doesn't work (gcc-3.4.4)

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;
 
J

Jonathan Mcdougall

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

James Kanze

You have to tell the compiler NestedClass::SubType is a type:
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

Ian Collins

James said:
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!
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top