View previous topic :: View next topic |
Author |
Message |
X-SoCiaL Apprentice
Joined: 15 Jul 2002 Posts: 160 Location: Filipstad/Sweden
|
Posted: Thu Mar 06, 2003 10:00 am Post subject: Function templates in C++? |
|
|
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 |
|
|
zaftro n00b
Joined: 07 Jan 2003 Posts: 39 Location: Melbourne, Australia
|
Posted: Thu Mar 06, 2003 10:53 am Post subject: |
|
|
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 |
|
|
X-SoCiaL Apprentice
Joined: 15 Jul 2002 Posts: 160 Location: Filipstad/Sweden
|
Posted: Thu Mar 06, 2003 1:11 pm Post subject: |
|
|
Thanx for the quick reply ... what is a newbie to do without you guys
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 |
|
|
Starfox Tux's lil' helper
Joined: 04 Sep 2002 Posts: 93
|
Posted: Fri Mar 07, 2003 10:28 am Post subject: Re: Function templates in C++? |
|
|
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 |
|
|
zhenlin Veteran
Joined: 09 Nov 2002 Posts: 1361
|
Posted: Fri Mar 07, 2003 1:23 pm Post subject: |
|
|
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 |
|
|
|
|
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
|
|