Anitesh
Anitesh

Reputation: 27

function template overload issue

Why am I getting an error saying:

  1. error C2668: 'max' : ambiguous call to overloaded function
  2. error C2780: 'const T &max(const T &,const T &,const T &)' : expects 3 arguments - 2 provided.

in the following code:

template<typename T>
inline T const& max(T const& i, T const& j)
{
  cout<<"Using template with 2 args."<<endl;
  return (i>j) ? i : j;
}

template<typename T>
inline T const& max(T const& i, T const& j, T const& k)
{
  cout<<"Using template with 3 args."<<endl;
  return max(max(i,j),k);
}

void main()
{
  cout<< ::max(1,2,3)<<endl;    
}

I have already defined the 2 argument template function before calling it.

Upvotes: 0

Views: 71

Answers (1)

ForEveR
ForEveR

Reputation: 55887

Remove using namespace std, since there is std::max, that takes part in search, thats why you get first error. Second error simply says, that there is unambiguous variant, but they should receive 3 args.

Upvotes: 2

Related Questions