passing pointer into template function

I'm trying to pass the pointer of a dynamic array into a template function, but it keeps telling me there is no matching function to call because the parameters I'm passing in are wrong. I can't figure out how to make the function accept the pointer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

//main
int main()
{
   srand(unsigned(time(NULL)));
   
   int size;
   int *list;
   int *listCopy;
   
   size = checkNum<int>();
   
   list = new int[size];
   listCopy = new int[size];
   
   generateRandom(listCopy, size);
   copyArray(list, listCopy, size);
   cout << "Original List\n";
   displayArray(list, size);

   delete [] list;
   delete [] listCopy;
   list = 0;
   listCopy = 0;
   
   return 0;
}

//functions
template <typename T>
int checkNum(){
   int size;
   
   cout << "Enter Size: ";
   cin >> size;
   
   while (size < 0 && !cin){
      cout << "Invalide input\n" << "Enter Size: ";
      cin >> size;
   }
   
   return size;
}


template <typename T>
void copyArray(T *list[], T *listCopy[], int size)
{
   for(int i = 0; i < size; i++)
   {
      list[i] = listCopy[i];
   }
}

template <typename T>
void displayArray(T *list[], int size)
{
   for(int i = 0; i < size; i++)
   {
      cout << list[i] << " ";
   }
   cout << endl;
}

template <typename T>
void generateRandom(T *list[], int size){
   int num = 0;
   
   for(int i = 0; i < size; i++)
   {
      num = rand() % 100;
      list[i] = num;
   }
   
}



//header
using namespace std;

template <typename T>
int checkNum();
template <typename T>
void copyArray(T* list[], T* listCopy[], int size);
template <typename T>
void displayArray(T* list[], int size);
template <typename T>
void generateRandom(T* list[], int size);
Your functions take arrays of pointers to (arrays of) T, while you are simply passing them pointers to (arrays of) T. In other words, the type of list, for example, is int*. You try to pass this to a function expecting a T*[]; there is no way to substitute a type into T to make them match. Perhaps you meant simply T list[], etc. in your functions? Additional, checkNum() doesn't need to be a template function as it doesn't use the template parameter at all.
yes I tried having the function parameter just T list[] and got a ton of linker errors, one for each of my functions:

Undefined symbols for architecture x86_64:
"void copyArray<int>(int*, int*, int)", referenced from:
_main in main.o

And yah you're right about checkNum not needing to be a template.
In regular functions I've always put the pointer in the prototype and function parameters and it worked fine. I don't understand why templates won't accept the pointer.
Try using T* list, as you are passing pointers to arrays. Or is that something you already tried?
Registered users can post here. Sign in or register to post.