Prev: RVO
Next: template
From: Michael Aaron Safyan on
kiran wrote:
> I have a very simple template swap function as follows:
>
> # include <iostream>
> using namespace std ;
>
> template <class T>
> void swap(T& m, T& n) {
> T temp ;
> temp = m;
> m = n ;
> n = temp ;
> }
>
> In the main function, I call this function with int parameters:
>
> int main() {
> int k=9, j=0 ;
> swap(k,j) ;
> return 0 ;
> }
>
> Seemingly simple, the program doesn't compile. Its gives an error
> saying call to swap(int&, int&) is ambiguous, the candidates being
> void swap(int& T, int& T) [T = int] and
> void std::swap(int& _Tp, int& _Tp) [_Tp=int]
>
> Googling didn't help either. I am little perplexed here as I am unable
> to grasp the error. Can someone throw some light on it. (The program
> compiles when I replace the call to swap by std::swap).
>
> Thanks and Regards,
> Kiran
>

The problem is the "using namespace std" directive; namespace std
already provides a definition of swap. You can fix the problem by
removing the using directive, by renaming your function, or by placing
your function in your own namespace and using its fully-qualified name.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 | 
Pages: 1
Prev: RVO
Next: template