call of overloaded function is ambiguous.

Hi,
All

I tried to run the following code on my gcc but encountered an error saying.
" 38:9: error: call of overloaded ‘swap(int&, int&)’ is ambiguous swap(a,b); "

Please enlighten me the concepts I am lacking..


Thanks
S.Das


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>

using namespace std;


//============================ IF I WANT TO ADD =======================
template < class T> 
void add(T &a, T &b)
{
T sum = a+b;
std::cout << "SUM = " << sum << "\n"; 
};



//============================ IF I WANT TO SWAP =====================
template <class U> void swap (U &a1, U &b1)
{

std::cout << "\n\n BEFORE SWAP :: A = " << a1 << " B= " << b1 ;

U temp =a1;
a1=b1;
b1=temp;

std::cout << "\n AFTER SWAP :: A= " << a1 << " B= " << b1 << "\n";
};

//===================================================================
//
int main()
{

int a=2;
int b=3;
add (a,b);
double ad=2.1;

double bd=3.5;
char ch_a='x';
char ch_b='y';

add (ad, bd);
swap(a,b);
//swap (ch_a,ch_b);


return 0;
}
It's because you are using using namespace std; so the compiler doesn't know if it should call your swap or std::swap.
Thanks Peter87 !!
Topic archived. No new replies allowed.