5

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;
            }
}
Pavitar
  • 4,282
  • 10
  • 50
  • 82
  • First make original signature proper. template < typename T > void Max( T& a, T& b, T& c) – Manoj R Oct 14 '10 at 15:57
  • In my case, it's caused by adding const to returnt type specilizatoin hence causing mismatch with primary template function . – FaceBro Sep 26 '17 at 16:09

2 Answers2

7

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?"

James McNellis
  • 348,265
  • 75
  • 913
  • 977
4

I ran into the same problem and fixed it by using typedef:

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