Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have written a function template and an explicitly specialized templated function which simply takes in 3 arguments and calculates the biggest among them and prints it.

The specialized function is causing an error,whereas the template works fine. But I want to work with char* type.

This is the error I get=> error: template-id ‘Max<>’ for ‘void Max(char, char, char)’ does not match any template declaration

Following is my code:

    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;
            }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
928 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need to take the pointers by reference:

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

That said, it would be better not to use an explicit specialization and instead just overload the function:

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

It's almost always a bad idea to specialize function templates. For more, see Herb Sutter's "Why Not Specialize Function Templates?"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...