小白程序猿Alex 2016-12-06 15:08 采纳率: 0%
浏览 1168

为什么加了const就模板函数的具体化编译不过?

这是编的过的代码:

 #include <iostream>
using namespace std;
#include <cstring>

template <typename T>
T maxn(T array[], int n);

template<> char* maxn(char* str[], int n);

int main()
{
    int ig[6] = {1,2,3,4,5,0};
    double dg[4] = {1.3, 4.8, 2.8, 3.2};
    char * cg[5] = {
        "alex is cool",
        "amy is nice",
        "dwdw",
        "al",
        "ddd"
    };


    cout << maxn(ig, 6) << endl;
    cout << maxn(dg, 4) << endl;
    cout << maxn(cg, 5) << endl;



    return 0;
}

template <typename T>
T maxn(T array[], int n)
{
    T max = array[0];
    for(int i = 0; i < n; ++i)
    {
        max = (max > array[i] ? max : array[i]);
    }

    return max;
}


template<> char* maxn(char* str[], int n)
{
    char * longest = str[0];
    for(int i = 0; i < n ; ++i)
    {
        if(strlen(longest) < strlen(str[i]))
        {
            longest = str[i];
        }
    }

    return longest;
}

这是编不过的代码:

#include <iostream>
using namespace std;
#include <cstring>

template <typename T>
T maxn(const T array[], int n);

template<> char* maxn(const char* str[], int n);

int main()
{
    int ig[6] = {1,2,3,4,5,0};
    double dg[4] = {1.3, 4.8, 2.8, 3.2};
    char * cg[5] = {
        "alex is cool",
        "amy is nice",
        "dwdw",
        "al",
        "ddd"
    };


    cout << maxn(ig, 6) << endl;
    cout << maxn(dg, 4) << endl;
    cout << maxn(cg, 5) << endl;



    return 0;
}

template <typename T>
T maxn(const T array[], int n)
{
    T max = array[0];
    for(int i = 0; i < n; ++i)
    {
        max = (max > array[i] ? max : array[i]);
    }

    return max;
}


template<> char* maxn(const char* str[], int n)
{
    char * longest = str[0];
    for(int i = 0; i < n ; ++i)
    {
        if(strlen(longest) < strlen(str[i]))
        {
            longest = str[i];
        }
    }

    return longest;
}

  • 写回答

2条回答 默认 最新

  • 熊猫爱吃肉 2016-12-06 18:47
    关注

    #include
    using namespace std;
    // #include

    template
    T maxn(const T array[], int n);

    template<> const char* maxn(const char* const str[], int n);

    int main()
    {
    int ig[6] = {1, 2, 3, 4, 5, 0};
    double dg[4] = {1.3, 4.8, 2.8, 3.2};
    const char * cg[5] = { //不加const是不会调用特化的模版的
    "alex is cool",
    "amy is nice",
    "dwdw",
    "al",
    "ddd"
    };

    cout << maxn(ig, 6) << endl;
    cout << maxn(dg, 4) << endl;
    cout << maxn(cg, 5) << endl;
    
    
    
    return 0;
    

    }

    template
    T maxn(const T array[], int n)
    {
    T max = array[0];
    for (int i = 0; i < n; ++i)
    {
    max = (max > array[i] ? max : array[i]);
    }

    return max;
    

    }

    template<> const char* maxn(const char* const str[], int n)
    {
    const char * longest = str[0];
    for (int i = 0; i < n ; ++i)
    {
    if (strlen(longest) < strlen(str[i]))
    {
    longest = str[i];
    }
    }

    return longest;
    

    }

    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)