Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 04-13-2005, 11:55 AM   #1
LQ Newbie
Registered: Mar 2005
Posts: 20

Rep: Reputation: 0
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.


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';


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............
I hope you all will help
Old 04-13-2005, 06:51 PM   #2
Registered: Feb 2005
Location: Christchurch, NZ
Distribution: Kubuntu Dapper 6.06
Posts: 43

Rep: Reputation: 15

not sure about the code problem but 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!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with templates ........ max_rsr Programming 4 04-22-2005 12:53 PM
c++ templates ashirazi Programming 5 07-30-2004 05:17 AM
Linking problem while using templates The_Nerd Programming 7 07-11-2004 09:11 PM
C++ and templates The_Nerd Programming 4 06-23-2004 01:15 PM
Templates MGLindsey Linux - Networking 0 07-02-2002 11:12 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:17 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration