LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-13-2005, 07:55 PM   #1
max_rsr
LQ Newbie
 
Registered: Mar 2005
Posts: 20

Rep: Reputation: 0
Need help with templates ........


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
 
Old 04-13-2005, 07:59 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

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

http://www.linuxquestions.org/rules.php

The original is here:
http://www.linuxquestions.org/questi...hreadid=312839
 
Old 04-13-2005, 09:00 PM   #3
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Rep: Reputation: 30
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

Last edited by ahwkong; 04-13-2005 at 09:02 PM.
 
Old 04-14-2005, 08:35 AM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

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

Last edited by deiussum; 04-14-2005 at 08:38 AM.
 
Old 04-22-2005, 12:53 PM   #5
max_rsr
LQ Newbie
 
Registered: Mar 2005
Posts: 20

Original Poster
Rep: Reputation: 0
Thanks guys

Thanks a lot guys . It really helped me..
 
  


Reply



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
Impress Templates logosys Linux - Software 1 09-13-2005 10:33 AM
problem with templates please help..... max_rsr Linux - Newbie 1 04-13-2005 06:51 PM
c++ templates ashirazi Programming 5 07-30-2004 05:17 AM
C++ and templates The_Nerd Programming 4 06-23-2004 01:15 PM
Templates MGLindsey Linux - Networking 0 07-02-2002 11:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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