Error: No matching function call to... Using a template

This is my first time using templates, and I'm trying to return an unknown data type from a function. I keep getting 2 errors:

F:\jkf\TM\TMP\main.cpp|13|error: no matching function for call to 'minMax(int, int, const char [4])'|

F:\jkf\TM\TMP\main.cpp|13|note: cannot convert '"min"' (type 'const char [4]') to type 'int'|


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <iomanip>
#include <typeinfo>
#include <string>

template <typename Temp>
Temp minMax(Temp, Temp, string);
using namespace std;

int main()
{
    minMax(1, 5, "min");
    //minMax("Hello", "Dolly", "max");
    //minMax('9', 'z', "min");
}

template <typename Temp>
Temp minMax(Temp A, Temp B, string C){
    Temp theMin, theMax;

    if(C == "min"){
        cout << "The minimum of " << A << ", " << "is ";
        if(A < B){
            cout << A << endl;
            return A;
        }
        else{
            cout << B << endl;
            return B;
        }
    }
    else{
         cout << "The maximum of " << A << ", " << "is ";
        if(A > B){
            cout << A << endl;
            return A;
        }
        else{
            cout << B << endl;
            return B;
        }
    }

}


Last edited on
Lines 6, 7 (note: using namespace std; appears later, on line 8)
1
2
3
template <typename Temp>
// Temp minMax(Temp, Temp, string);
Temp minMax(Temp, Temp, std::string);



Perhaps write it as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>

template < typename T > T minMax( T a, T b, std::string what ) ;

int main()
{
    minMax( 1, 5, "min" );
    minMax( std::string("Hello"), std::string("Dolly"), "max");
    minMax( '9', 'z', "min" );
}

template < typename T > const T& minv( const T& a, const T& b )
{ return a<b ? a : b  ; }

template < typename T > const T& maxv( const T& a, const T& b )
{ return a<b ? b : a  ; }

template < typename T > const T& minmaxv( const T& a, const T& b, const std::string& what )
{
    if( what == "min" ) return minv(a,b) ;
    else return maxv( a, b ) ;
}

template < typename T > T minMax( T a, T b, std::string what )
{
    const T& result = minmaxv( a, b, what ) ;

    if( what == "min" )  std::cout << "the minimum of " << a << " and " << b << " is: " << result << '\n' ;
    else if( what == "max" )  std::cout << "the maximum of " << a << " and " << b << " is: " << result << '\n' ;
    else std::cerr << "error: neither 'min' nor 'max'\n" ;

    return result ;
}

http://coliru.stacked-crooked.com/a/76bfde26cc85a77c
I can't believe I missed that! Thank you!!
Topic archived. No new replies allowed.