简体   繁体   中英

template id does not match any template declaration

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
}

如果Tchar* ,则签名应为char* max<char*>(char* array[], int items)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM