1

The following fails to compile (with gcc 4.2.1 on Linux, anyway):

template< typename T >
class Foo
{
public:
   typedef int FooType;
};

void
ordinary()
{
   Foo< int >::FooType bar = 0;
}

template< typename T >
void
templated()
{
   Foo< T >::FooType bar = T( 0 );
}

int main( int argc, char **argv )
{
   return 0;
}

The problem is with this line:

   Foo< T >::FooType bar = 0;

...and the compiler makes this complaint:

foo.c: In function ‘void templated()’:

foo.c:22: error: expected `;' before ‘bar’

Normally one sees this when a type hasn't been declared, but as far as I can tell, Foo< T >::FooType should be perfectly valid inside templated().

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
atomicpirate
  • 657
  • 5
  • 12

1 Answers1

2

use typename:

  typename Foo< T >::FooType bar = 0;

See this for why typename is needed.

Community
  • 1
  • 1
amit kumar
  • 20,438
  • 23
  • 90
  • 126