Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 807
user9985127 Avatar asked Jan 30 '26 08:01

user9985127


1 Answers

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?

like image 115
JeJo Avatar answered Jan 31 '26 22:01

JeJo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!