ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
please help me with this ....
I can't seem to get my code work...
the following code doen't seem to work.
Here's the code.
#include<iostream>
using namespace std;
template <class T>
T swap(T &a , T &b)
{
T c ;
c = a;
a = b;
b = c;
}
int main()
{
int a = 1,b = 2;
float c = 1.0,d = 2.0;
char e = 'A',f = 'B';
swap(a,b);
swap(c,d);
swap(e,);
}
these are the errors I am receiving
template1.cpp: In function `int main()':
template1.cpp:22: error: call of overloaded `swap(int&, int&' is ambiguous
template1.cpp:8: error: candidates are: T swap(T&, T& [with T = int]
/usr/include/c++/3.3.3/bits/stl_algobase.h:121: error: void
std::swap(_Tp&, _Tp& [with _Tp = int]
template1.cpp:23: error: call of overloaded `swap(float&, float&' is ambiguous
template1.cpp:8: error: candidates are: T swap(T&, T& [with T = float]
/usr/include/c++/3.3.3/bits/stl_algobase.h:121: error: void
std::swap(_Tp&, _Tp& [with _Tp = float]
template1.cpp:24: error: call of overloaded `swap(char&, char&' is ambiguous
template1.cpp:8: error: candidates are: T swap(T&, T& [with T = char]
/usr/include/c++/3.3.3/bits/stl_algobase.h:121: error: void
std::swap(_Tp&, _Tp& [with _Tp = char]
Please Help.. I need to finish the chapter on templates as soon as possible
And by the way if u know any websites which would help me with C++ please specify
especially the one which would help to study C++ inside out............
Thanks
I hope you all will help
Please do not post the same thread in more than one forum. Picking the most relevant forum and posting it once there makes it easier for other members to help you and keeps the discussion all in one place.
Ok, since Tinkster has played the "bad guy" I can play a "good guy" here :-)
You need to change the code in the following way:
Code:
#include<iostream>
template <typename T>
T swap(T &a , T &b)
{
T c ;
c = a;
a = b;
b = c;
}
int main()
{
int a = 1,b = 2;
float c = 1.0,d = 2.0;
char e = 'A',f = 'B';
swap<int>(a,b);
swap<float>(c,d);
swap<char>(e,f);
}
1) do not use namespace std (in this case only). It confuses the compiler because there is already a std::swap which clash with yours
2) you did not instantiate your swap function properly. see "swap<char>" above?
3) And it is better to use typename than class
You don't need to use the template syntax to call the swap functions above, although doing so will explicitly let the compiler know what specialization you want to use.
As you pointed out the real problem is that the two versions of the swap function were clashing. Another way to get around that would have been to append :: in front of your swap to indicate the swap from the global namespace. Getting rid of the using namespace std is probably a cleaner solution, though.
Another minor point is that you are defining your swap function as returning a T, but you never actually return anything. The compiler doesn't seem to generate any warnings or errors about this, so it's probably not a big deal, though.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.