Search code examples
c++arraysc++11templatesclass-template

Using typedef types in definition


I have a class as follows,

template<size_t N>
class A 
{
   typedef int(*ARRAY)[N];
   array getArray();

   ARRAY array;
};

Then how can I use the type ARRAY in the definition? I have tried this

template<size_t N>
ARRAY A<N>::getArray()
{
   return array;
}

But, it tells me that ARRAY is not defined.


Solution

  • ARRAY is depended name, therefor you need to use the typename keyword before it, outside the class scope.

    template<size_t N>
    typename A<N>::ARRAY A<N>::getArray()
    //^^^^^^^^^^^^^
    {
       return array;
    }
    

    or using trailing return

    template<size_t N>
    auto A<N>::getArray() -> ARRAY
    //                 ^^^^^^^^^^^^^
    {
       return array;
    }
    

    In addition, you have a typo in your class member declaration. The

    array getArray();
    

    should be

    ARRAY getArray();
    

    However, is there any reason that you do not want to use the std::array?