繁体   English   中英

模板化函数..错误:template-id与任何模板声明都不匹配

[英]Templated Functions.. ERROR: template-id does not match any template declaration

我编写了一个函数模板一个显式专用的模板化函数 ,它只需要3个参数并计算其中最大的参数并打印出来。

专用函数导致错误,而模板工作正常。 但我想使用char *类型

这是我得到的错误=> error: template-id 'Max<>' for 'void Max(char, char, char)' does not match any template declaration

以下是我的代码:

    template <typename T>
    void Max(T& a,T& b,T& c)
    {
            if(a > b && a >> c)
            {
                    cout << "Max: " << a << endl;
            }
            else if(b > c && b > a)
            {
                    cout << "Max: " << b << endl;
            }
            else
            {
                    cout << "Max: " << c << endl;
            }
    }

    template <>
    void Max(char* a,char* b,char* c)
    {
            if(strcmp(a,b) > 0 )
            {
                    cout << "Max: " << a << endl;
            }
            else if(strcmp(b,c) > 0)
            {
                    cout << "Max: " << b << endl;
            }
            else
            {
                    cout << "Max: " << b << endl;
            }
}

您需要通过引用获取指针:

template <> 
void Max(char*& a,char*& b,char*& c) 

也就是说,最好不要使用显式特化,而只是重载函数:

void Max(char* a, char* b, char* c)

专门化功能模板几乎总是一个坏主意。 有关更多信息,请参阅Herb Sutter的“为什么不专业化功能模板?”

我遇到了同样的问题,并使用typedef修复它:

typedef char * charPtr;
template <>
void Max(charPtr &a, charPtr &b, charPtr &c)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM