templates

Having some frustration here, attempting to define a templated function that accepts multiple data types, but Im doing something wrong with the template. Any help is mucho appreciated.

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
#include <iostream>
#include <string>
using namespace std;

template<typename T, typename B, typename C>
T DisplayMin(B temp1[], C temp2[], int size);


int main(){
 const int size_t = 5;
 string names[size_t];
 int prices[size_t];

 cout << "enter first array values.\n";
   for (int i = 0; i<size_t; i++){
   cin >> names[i];
   }
   cout << "enter 2nd array values.\n";

   for (int i = 0; i<size_t; i++){
   cin >> prices[i];
   }
int DisplayMin(names, prices, size_t);
 return 0;
}


template<typename T, typename B, typename C>
T DisplayMin(B temp1[], C temp2[], int size){
    if(isdigit(temp1[0])){
        int val = temp1[0];
        int counter = 0;
         for (int i = 0; i < size; i++){
             if (val < temp1[i]){
                 val = temp1[i];
             }
             counter +=1;
             }
             cout << "The min value of " << val << " belongs to "<< temp2[counter] << ".\n";
             return;
    }
    else if(isdigit(temp2[0])){
        int val = temp2[0];
        int counter = 0;
         for (int i = 0; i < size; i++){
             if (val < temp2[i]){
                 val = temp2[i];
             }
             counter +=1;
             }
             cout << "The min value of " << val << " belongs to "<< temp1[counter] << ".\n";
             return;
    }
    else {
        cout << "Neither arrays contain a min numeric value.\n";
        return;
    }
  }
Well since most of the problems are not template related I suggest you start by getting a non-template class to work before you throw in the complexities of templates. Also when you do get to templates start with a template that only requires one templated parameter.

Registered users can post here. Sign in or register to post.