Ian Fiddes
Ian Fiddes

Reputation: 3001

Error when initializing function with typename after typedef in templated class

The code below does not compile:

//in definition
typedef double value_type;

//in implementation
template <typename T>
typename value_type sequence<T>::current( )
{
    return data[used-1];
}

Replacing "typename value_type" with "double" causes the code to compile and work as expected. Why can't I use typename value_type in place of double, if I have assigned value_type to be equivalent to double?

Upvotes: 0

Views: 44

Answers (1)

Andy Prowl
Andy Prowl

Reputation: 126432

Assuming that "in definition" means "in the definition of the sequence<> class template", and "in implementation" means "in the definition of the current() member function of the sequence<> class template", then what you have to write is:

template <typename T>
typename sequence<T>::value_type sequence<T>::current( )
//       ^^^^^^^^^^^^^
{
    return data[used-1];
}

Also keep in mind, that unless you are using explicit specializations, the definition of member functions of a class template should be put in the same header that contains the definition of the class template.

Upvotes: 3

Related Questions