Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
显式专门化-模板id与任何模板声明都不匹配 我在C++程序中有明确的专门化问题_C++_C++11_Templates_Specialization_Explicit - Fatal编程技术网

显式专门化-模板id与任何模板声明都不匹配 我在C++程序中有明确的专门化问题

显式专门化-模板id与任何模板声明都不匹配 我在C++程序中有明确的专门化问题,c++,c++11,templates,specialization,explicit,C++,C++11,Templates,Specialization,Explicit,我想对char*类型进行专门化,该类型返回最长char数组的地址,但我不断遇到错误: C:\Users\Olek\C++\8_1\main.cpp|6|error: template-id 'maxn<char*>' for 'char* maxn(char*)' does not match any template declaration| C:\Users\Olek\C++\8_1\main.cpp|38|error: template-id 'maxn<char*>

我想对char*类型进行专门化,该类型返回最长char数组的地址,但我不断遇到错误:

C:\Users\Olek\C++\8_1\main.cpp|6|error: template-id 'maxn<char*>' for 'char* maxn(char*)' does not match any template declaration|
C:\Users\Olek\C++\8_1\main.cpp|38|error: template-id 'maxn<char*>' for 'char* maxn(char*)' does not match any template declaration|
C:\Users\Olek\C++\8_1\main.cpp | 6 |错误:“char*maxn(char*)”的模板id“maxn”与任何模板声明都不匹配|
C:\Users\Olek\C++\8_1\main.cpp | 38 |错误:“char*maxn(char*)”的模板id“maxn”与任何模板声明都不匹配|
这是程序的代码

#include <iostream>

template <typename T>
T maxn(T*,int);

template <> char* maxn<char*>(char*);

const char* arr[5]={
    "Witam Panstwa!",
    "Jak tam dzionek?",
    "Bo u mnie dobrze",
    "Bardzo lubie jesc slodkie desery",
    "Chociaz nie powinienem",
};

using namespace std;

int main()
{
    int x[5]={1,4,6,2,-6};
    double Y[4]={0.1,38.23,0.0,24.8};
    cout<<maxn(x,5)<<endl;
    cout<<maxn(Y,4)<<endl;
    return 0;
}

template <typename T>
T maxn(T* x,int n){
    T max=x[0];
    for(int i=0;i<n;i++){
        if(x[i]>max)
            max=x[i];
    }
    return max;
}

template <>
char* maxn<char*>(char* ch){
    return ch;
}
#包括
样板
T最大值n(T*,int);
模板字符*最大值(字符*);
常量字符*arr[5]={
“维塔姆·潘斯特瓦!”,
“Jak tam dzionek?”,
“Bo u mnie dobrze”,
“Bardzo lubie jesc slodkie应得”,
“Chociaz nie powinienem”,
};
使用名称空间std;
int main()
{
int x[5]={1,4,6,2,-6};
双Y[4]={0.1,38.23,0.0,24.8};

cout您的模板返回类型
T
,并采用类型
T*
,但是您的专门化返回的类型与它采用的类型相同,因此它不匹配

如果您专门处理
T=char*
,那么它需要获取一个T*(char**)并返回一个
char*
,或者专门处理
char

template <typename T>
T maxn(T*,int);

template <> char maxn<char>(char*, int);
template <> char* maxn<char*>(char**, int);

int main() {
    char * str;
    maxn(str, 5);
    maxn(&str, 6);
}
模板
T最大值n(T*,int);
模板char maxn(char*,int);
模板char*maxn(char**,int);
int main(){
char*str;
maxn(str,5);
maxn(&str,6);
}

您希望哪个模板
模板char*maxn(char*);
专门化?maxn模板,模板T maxn(T*x,int n)哦,等等,我是否还应该添加第二个参数?(int n)?。它不起作用。谢谢回答