LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   problem with templates please help..... (https://www.linuxquestions.org/questions/linux-newbie-8/problem-with-templates-please-help-312839/)

max_rsr 04-13-2005 11:55 AM

problem with templates please help.....
 
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

saltire 04-13-2005 06:51 PM

Um,

not sure about the code problem but Cprogramming.com has some good tutorials on everything C++ related.
In my experience with Java, the compiler error report usually points to the problem, or fairly close to it, so try working through the errors. From the report you've supplied, it looks like the overloading of the swap() method is the problem.

You could try posting this to the Programming Forum here at LQ, which can be found near the bottom of the LQ Forums title page.

Good Luck!


All times are GMT -5. The time now is 09:12 PM.