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

标签 c++ arrays templates

我正在尝试编写一个函数模板,该模板返回值数组中的最大值。它应该还有一个专门的模板,它接受一个指向字符的指针数组,并返回该数组元素所指向的最大字符串的地址。我收到错误“[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
}

最佳答案

如果Tchar* ,那么签名应该是char* max<char*>(char* array[], int items)

关于c++ - 模板 ID 与任何模板声明都不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31041887/

相关文章:

c++ - Visual Studio 2010 项目中引用的库

c++ - Qt中是否需要一个一个查看才能知道一个组中选中了哪个radiobutton

php - 如何反转数组?

java - 声明数组增量元素的快速方法 - Java

C++新手问题: designing a function to return a vector of either string or double--overload or template?

c++ - 专门化模板类时的继承

c++ - 如何查找给定字符串的 ENUM 类型?

c++ - 好的 C++11 信息,除了维基百科?

javascript - 将其值移动到另一个数组后显示整个数组

c# - T4 模板是否在每次请求时都运行?