#include <iostream>
using namespace std;
int max (int a, int b)
{
return a<b?b:a;
}
template <typename T> T max (T a, T b)
{
return a<b?b:a;
}
template <typename T> T max (T a, T b, T c)
{
return max (max(a,b), c);
}
int main()
{
// The call with two chars work, flawlessly.
:: max ('c', 'b');
// This call with three chars produce the error listed below:
:: max ('c', 'b', 'a');
return 0;
}
Error:
error: call of overloaded ‘max(char&, char&)’ is ambiguous
Shouldn't this max ('c', 'b', 'a')
call the overloaded function with three arguments?
Thing is, there is already a max
in std
, and you are saying using namespace std;
:
template <class T> const T& max ( const T& a, const T& b );
So your max ('c', 'b', 'a')
is called fine; the problem is inside it.
template <typename T> T max (T a, T b, T c)
{
return max (max(a,b), c); /* Doesn't know which max to pick. */
}
I don't know why max
is available since you didn't include algorithm
, but apparently it is.
If you want to keep that using
at the top:
template <typename T> T max (T a, T b, T c)
{
return ::max(::max(a, b), c);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With