2

I have this class

template <class T> class dynamic_array
  {

and this function:

void dynamic_array::randorder()
  {
  srand(time(NULL));
  int *ap;
  for(ap=array;k!=array+size;++k){*k=rand();} 
  }

The compiler is complaining about my function - "not having template parameters". How do I add this in?

2 Answers2

8
template <class T>
void dynamic_array<T>::randorder()
{
  srand(time(NULL));
  for(int *ap = array; k != array + size; ++k)
  {
    *k = rand();
  }
}
Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
3

It should be

template <class T>
void dynamic_array<T>::randorder()
  {
...
  }

Also, keep in mind that you have to put the definition into a header file if you need to use it in different .cpp files.

I suppose array must be a data member of type T, so the following doesn't apply here. But in general if you see that some member functions of a class template do not depend on the template parameters, it makes sense to factor them out into a non-template base class. It reduces the size of the executable and makes your life easier.

Dima
  • 38,860
  • 14
  • 75
  • 115