Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call of overloaded ‘max(char&, char&)’ is ambiguous

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

like image 773
Aquarius_Girl Avatar asked Dec 17 '22 09:12

Aquarius_Girl


1 Answers

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.

EDIT

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);
} 
like image 126
cnicutar Avatar answered Jan 25 '23 07:01

cnicutar