What is the difference between these two codes I just don't understand what the problem is.
Version 1 which works perfectly:
template <class T>
T max (T& a, T& b)
{
return a < b ? b : a;
}
int main()
{
auto i = 2;
auto j = 3;
cout << "max(i, j): " << max(i, j) << endl;
return 0;
}
Version 2 which does not work:
template <class T>
T max (T& a, T& b)
{
return a < b ? b : a;
}
int main()
{
cout << "max(i, j): " << max(44, 33) << endl;
return 0;
}
So my question is why does the second version of the same code not work. Can someone tell me what the problem is with it. In the second version the max(44, 33)
are underlined in red. The error message reads:
Error C2664: 'T max <int>(T &, T &)': cannot convert argument 1 from 'int' to 'int&'(15)
I really don't understand this error and I really want someone to explain why this is happening and how can I fix this. Also when you hover under the max is reads: no instance of function template max matches the argument list argument types are: (int, int)
.