Reputation: 243
I am trying to write a function template that returns the maximum value out of an array of values. It is supposed to also have a specialized template that accepts an array of pointers to chars and returns the address of the largest string pointed to by an element of the array. I get the error "[Error] template-id 'max' for 'char max(char**, int)' does not match any template declaration". I'm really uncertain as to what is wrong in my template specialization, also I do not want to overload the function, I want to know why this code isn't working.
my code:
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
//returns the maximum value out of an array
template `<typename T>`
T max(T array[], int items);
//returns returns address of largest string pointed to by an element in the array
template <>
char * max <char>(char * array[], int items);
int elemcout();
int main(){
double list [] = {2.4, 5.7, 3.6, 8.1, 10.6, 15.7, 3.1415};
int items []= {20,50,10,30,40};
cout << max(list, 7) <<endl <<max(items, 5);
}
template <typename T>
T max (T array[] ,int items){
T cmax = 0;
for (int i = 0; i < items; i++){
if (array[i] > cmax)
cmax = array[i];
}
return cmax;
}
template <>
char * max <char>(char array[], int items){
//was working on this but got template missmatch error
}
Upvotes: 2
Views: 2481
Reputation: 1172
If T
is char*
, then the signature should be char* max<char*>(char* array[], int items)
Upvotes: 3