Ambiguous call to overloaded function?

So this is my program:

#include<iostream>
using namespace std;
void swap(int, int);
void readpair(int, int);
bool multiple(int, int);
void main()
{
int k, z, x;
cout<<"Enter Number of pairs:";
cin>>k;
for(z=1; z<=k; z++)
{
readpair(z, x);
if(multiple(z, x))
cout<<z<<x<<"It is a multiple"<<endl;
else
cout<<z<<x<<"It is not a multiple"<<endl;
}
}
void swap(int& a, int& b)
{
int c;
c=a;
a=b;
b=c;
}
void readpair(int& d, int& e)
{
cout<<"Enter first number";
cin>>d;
while(d<0)
{
cout<<"error";
cin>>d;
}
cout<<"Enter second number";
cin>>e;
while(e<0)
{
cout<<"error";
cin>>e;
}
if(e<d)
swap(d, e);
}
bool multiple(int o, int j)
{
if(j%o==0)
return(true);
else
return(false);
}
but then I get a red line underlining readpair and swap and it says error ambiguous call to overloaded functions?
Whats wrong?
Thanks
You are defined readpair() and swap() as taking references; however, you have declaried them as taking integers (by value.)

Just change the declarations above to use the references.
Last edited on
Thank you so much!
I meet the problem some days ago, it is because of the function swap(),At that time ,I use template to make more flexible function.That case happened,since swap is defined in cmath.h,if you change swap to Swap, that case would not happen.
@zhouhaibing089.

there is no any need to name the user-defined function as Swap. The problem you encountered is the result of using directive using namespace std;. So if you use unqualfied name swap then the compiler does not know which function to select. However you can specify qualified name and in this case the compiler will select required function. For example

::swap( a, b ); // the user-defined function will be used
std::swap( a, b ); // the standard function will be used
Last edited on
Topic archived. No new replies allowed.