Ambiguous Overloaded Function

Calling swap(first, second) gives me an error: call of overloaded 'swap(double&, double&)' is ambiguous. I'm not sure how this call is ambiguous when first and second are doubles and not pointers to anything. Also, the first swap() takes in two arguments, but the second one takes in three.

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
51
52
53
54
  const int Arsize = 10;

template <class Any>
void swap(Any &, Any &);

template <class Any>
void swap(Any*, Any*, int sizer);

template<class Any>
void show(Any*, int sizer);

using namespace std;


int main(void)
{
    double first = 3;
    double second  = 4.6;
    cout << "First: " << first << " Second: " << second << "\n";
    swap(first, second);
    cout << "First: " << first << " Second: " << second << "\n";
    int firs[Arsize] = {0,4,1.6,9,1,9,7,6};
    int secon[Arsize] = {0,2.4,1,4,2,0,0,1};
    show(firs, Arsize);
    show(secon, Arsize);
    swap(firs, secon, int(Arsize));
    show(firs, Arsize);
    show(secon, Arsize);
    return 0;
}

template <class Any>
void swap(Any&a, Any&b)
{
    Any temp = a;
    a = b;
    b = temp;
}
template <class Any>
void swamp(Any*pa, Any*pb, int sizer)
{
    for(int i = 0; i<sizer; i++)
        pa[i]=pb[i];
}
template <class Any>
void show(Any*pa, int sizer)
{
    cout << pa[0] << pa[1] << "/"
    << pa[2] << pa[3] << "/";
    for(int i = 0; i<4; i++)
        cout << pa[i];
    cout << "\n";
}
Last edited on
Which call(s) are ambiguous exactly?
It really helps if you tell us which of those calls is giving you the error.

EDIT:

Oh wait... okay I see it.

This is a namespace issue. Your swap function is conflicting with std::swap because you are doing using namespace std;

This is exactly why using namespace std; is discouraged. The whole point of having namespaces is to avoid name conflicts. When you dump the entire std namespace into the global namespace, it defeats the entire point of putting it in a namespace to begin with.

Either name your swap function something else... or put it in a namespace.... or don't dump std::swap into the global namespace.
Last edited on
Calling swap(first, second)
Topic archived. No new replies allowed.