Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Function templates in C++?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
X-SoCiaL
Apprentice
Apprentice


Joined: 15 Jul 2002
Posts: 160
Location: Filipstad/Sweden

PostPosted: Thu Mar 06, 2003 10:00 am    Post subject: Function templates in C++? Reply with quote

Im trying to get a grip on function templates. I got an example out of a book but I cant seem to get it to pass g++.

Here is the sourcecode:
Code:
// funtemplate.cxx --- using a function template
#include <iostream>
using namespace std;

template <class Any>
void swap(Any &a, Any &b);

int main()
{
   int i = 10;
   int j = 20;
   
   cout << "i, j = " << i << ", " << j << "\n";
   cout << "Using compiler-generated int swapper:\n";
   swap(i,j);
   cout << "Now i, j = " << i << ", " << j << "\n";
   
   double x = 24.5;
   double y = 81.7;
   
   cout << "x, y = " << x << ", " << y << "\n";
   cout << "Using compiler-generated double swapper:\n";
   swap(x,y);
   cout << "Now x, y = " << x << ", " << y << "\n";
   
   return 0;
}

template <class Any>
void swap( Any &a, Any &b)
{
   Any temp;
   temp = a;
   a = b;
   b = temp;
}


And here is what g++ says:

Code:
ntr@worker ch8 $ g++ funtemplate.cxx -o funtemplate
funtemplate.cxx: In function `int main()':
funtemplate.cxx:15: call of overloaded `swap(int&, int&)' is ambiguous
funtemplate.cxx:6: candidates are: void swap(Any&, Any&) [with Any = int]
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.2/include/g++-v3/bits/stl_algobase.h:121:
                  void std::swap(_Tp&, _Tp&) [with _Tp = int]
funtemplate.cxx:23: call of overloaded `swap(double&, double&)' is ambiguous
funtemplate.cxx:6: candidates are: void swap(Any&, Any&) [with Any = double]
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.2/include/g++-v3/bits/stl_algobase.h:121:
                  void std::swap(_Tp&, _Tp&) [with _Tp = double]
funtemplate.cxx:36:2: warning: no newline at end of file


Any 'guru's' out there that mind to help me out?

/Roger
_________________
~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~
: Would I do it if I couldnt do it with a computer?
~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~
Back to top
View user's profile Send private message
zaftro
n00b
n00b


Joined: 07 Jan 2003
Posts: 39
Location: Melbourne, Australia

PostPosted: Thu Mar 06, 2003 10:53 am    Post subject: Reply with quote

Hey,

In the g++ error output:
Code:
ntr@worker ch8 $ g++ funtemplate.cxx -o funtemplate
funtemplate.cxx: In function `int main()':
funtemplate.cxx:15: call of overloaded `swap(int&, int&)' is ambiguous
funtemplate.cxx:6: candidates are: void swap(Any&, Any&) [with Any = int]
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.2/include/g++-v3/bits/stl_algobase.h:121:
                  void std::swap(_Tp&, _Tp&) [with _Tp = int]
funtemplate.cxx:23: call of overloaded `swap(double&, double&)' is ambiguous
funtemplate.cxx:6: candidates are: void swap(Any&, Any&) [with Any = double]
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.2/include/g++-v3/bits/stl_algobase.h:121:
                  void std::swap(_Tp&, _Tp&) [with _Tp = double]
funtemplate.cxx:36:2: warning: no newline at end of file


it says that a function "swap" has already been declared in the std namespace.

So just change your functions name "swap" to "swappy" or something like that. Or instead of having "using namespace std;" you could have "using std::cout;".

Cheers,

zaftro
_________________
You may well be reading my signature. Don't be alarmed.

JID: dan.farrell@jabber.zim.net.au
Back to top
View user's profile Send private message
X-SoCiaL
Apprentice
Apprentice


Joined: 15 Jul 2002
Posts: 160
Location: Filipstad/Sweden

PostPosted: Thu Mar 06, 2003 1:11 pm    Post subject: Reply with quote

Thanx for the quick reply ... what is a newbie to do without you guys :wink:

It's not always that easy to understand the cryptic outputs from gcc/g++

/Roger
_________________
~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~
: Would I do it if I couldnt do it with a computer?
~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~'¨'~
Back to top
View user's profile Send private message
Starfox
Tux's lil' helper
Tux's lil' helper


Joined: 04 Sep 2002
Posts: 93

PostPosted: Fri Mar 07, 2003 10:28 am    Post subject: Re: Function templates in C++? Reply with quote

Ther is another error!!!!
See here:

// funtemplate.cxx --- using a function template
#include <iostream>
using namespace std;

template <class Any>
void swap(Any &a, Any &b);

int main()
{
int i = 10;
int j = 20;

cout << "i, j = " << i << ", " << j << "\n";
cout << "Using compiler-generated int swapper:\n";
swap<int>(i,j); // remember calling a template!!!!
cout << "Now i, j = " << i << ", " << j << "\n";

double x = 24.5;
double y = 81.7;

cout << "x, y = " << x << ", " << y << "\n";
cout << "Using compiler-generated double swapper:\n";
swap<int>(x,y); // remember calling a template!!!!
cout << "Now x, y = " << x << ", " << y << "\n";

return 0;
}

template <class Any>
void swap( Any &a, Any &b)
{
Any temp;
temp = a;
a = b;
b = temp;
}
Back to top
View user's profile Send private message
zhenlin
Veteran
Veteran


Joined: 09 Nov 2002
Posts: 1361

PostPosted: Fri Mar 07, 2003 1:23 pm    Post subject: Reply with quote

This works much better:
Code:

#include <iostream>
using namespace std;
                                                                                                                                               
template <class Any>
void myswap(Any &a, Any &b);
                                                                                                                                               
int main()
{
int i = 10;
int j = 20;
                                                                                                                                               
cout << "i, j = " << i << ", " << j << "\n";
cout << "Using compiler-generated int swapper:\n";
myswap<int>(i,j); // remember calling a template!!!!
cout << "Now i, j = " << i << ", " << j << "\n";
                                                                                                                                               
double x = 24.5;
double y = 81.7;
                                                                                                                                               
cout << "x, y = " << x << ", " << y << "\n";
cout << "Using compiler-generated double swapper:\n";
myswap<double>(x,y); // remember calling a template!!!!
cout << "Now x, y = " << x << ", " << y << "\n";
                                                                                                                                               
return 0;
}
                                                                                                                                               
template <class Any>
void myswap( Any &a, Any &b)
{
Any temp;
temp = a;
a = b;
b = temp;
}


And there is an existing function called swap in the standard namespace which should do exactly the same thing.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum