When I compile this code I get an error saying
call of overloaded
swap(int&, int&)
is ambiguous
but I've written only one swap function here.
Can you tell me why the function was ambiguous and what changes I need to do to run the program correctly?
using namespace std;
template <class T>
void swap(T& x, T& y)
{
T temp;
temp = x;
x = y;
y = temp;
}
int main()
{
int a, b;
cout << "Enter two elements: ";
cin >> a;
cin >> b;
swap(a, b);
cout << "a is "<<a << '\t'<<"b is " << b << std::endl;
return 0;
}
Why was the swapping function overloaded even though it has only only swap function?