模板ID与任何模板声明都不匹配

时间:2015-06-25 05:28:22

标签: c++ arrays templates

我正在尝试编写一个函数模板,它返回值数组中的最大值。它应该还有一个专用模板,它接受一个指向字符的指针数组,并返回数组元素指向的最大字符串的地址。我收到错误“[错误] template-id'max'为'char max(char **,int)'与任何模板声明都不匹配”。我真的不确定我的模板专业化有什么问题,我也不想重载函数,我想知道为什么这段代码不起作用。

我的代码:

#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
}

1 个答案:

答案 0 :(得分:3)

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

相关问题