簡體   English   中英

模板ID與任何模板聲明都不匹配

[英]template id does not match any template declaration

我正在嘗試編寫一個函數模板,該函數模板從值數組中返回最大值。 還應該有一個專門的模板,該模板接受一個指向chars的指針數組,並返回該數組元素所指向的最大字符串的地址。 我收到錯誤“ [char max(char **,int)”的[[Error] template-id'max'與任何模板聲明都不匹配”。 我真的不確定模板專業化中有什么問題,也不想重載該函數,我想知道為什么這段代碼無法正常工作。

我的代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM