#include <iostream>
using namespace std;
template <class any>
void swap(any a, any b);
int main()
{
int x = 10;
int y = 20;
swap(x,y);
return 0;
}
template <class any>
void swap(any a, any b)
{
Any temp;
temp = a;
a = b;
b = temp;
};
Need some help
Hi everyone.
Can anyone please tell me why this isnt working:
When i use the swap(x,y) function i get the following errors:
error C2667: 'swap' : none of 2 overload have a best conversion
error C2668: 'swap' : ambiguous call to overloaded function
Im using Visual Studio 6.0. Does anyone know what im doing wrong?
Thanks.
[edited by - Aldion on April 28, 2002 12:06:48 PM]
You got a declaration for an Any object in your swap fuction. The template is declared for any as opposed to Any.
Hope that''s it.
"640K ought to be enough for anybody."
-- Bill Gates, 1981
Hope that''s it.
"640K ought to be enough for anybody."
-- Bill Gates, 1981
"640K ought to be enough for anybody." -- Bill Gates, 1981
First, let me point out that your function will not swap two variables. You have to pass values by reference, like so:
void swap(any& a, any& b);
Second, the error you''re getting means that there are two templated swap functions and the compiler cannot choose between the two. I''ll guess that''s because STL has a swap function that is declared in the manner I wrote above, and you''re using namespace std. To fix this, try removing the using declaration or name your function something else.
void swap(any& a, any& b);
Second, the error you''re getting means that there are two templated swap functions and the compiler cannot choose between the two. I''ll guess that''s because STL has a swap function that is declared in the manner I wrote above, and you''re using namespace std. To fix this, try removing the using declaration or name your function something else.
---visit #directxdev on afternet <- not just for directx, despite the name
quote: Original post by IndirectX
First, let me point out that your function will not swap two variables. You have to pass values by reference, like so:
void swap(any& a, any& b);
Oh, thats true. I wrote the code again in the forum post, didnt paste the program with the code in it (its a bit longer. =) ) Thats the reason for all the code errors.
quote:
Second, the error you''re getting means that there are two templated swap functions and the compiler cannot choose between the two. I''ll guess that''s because STL has a swap function that is declared in the manner I wrote above, and you''re using namespace std. To fix this, try removing the using declaration or name your function something else.
Yep, now it works. Thanks!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement