我正在尝试编写一个函数模板,该模板返回值数组中的最大值。它应该还有一个专门的模板,它接受一个指向字符的指针数组,并返回该数组元素所指向的最大字符串的地址。我收到错误“[Error] template-id 'max' for '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
}
最佳答案
如果T
是 char*
,那么签名应该是char* max<char*>(char* array[], int items)
关于c++ - 模板 ID 与任何模板声明都不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31041887/