C++函数摸板

来源:百度知道 编辑:UC知道 时间:2024/07/04 06:21:07
以下是C++ Primer中的一个范例,不过在win-TC下不能通过,
函数的功能是调用函数摸板,对两对不同类型的数的值互换.
小弟刚学到这部分内容,望高手指点,错在什么地方了.
#include "iostream"
template <class Any>
void swap(Any &a, Any &b);
int main()
{
using namespace std;
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 int swapper: \n";
swap(x,y);
cout << "Now x,y=" << x << ". " << y <&l

namespace std 里有这个函数模板的定义,所以你重复定义了

改为swapp就可以了

#include <iostream>

template <class Any>
void swapp(Any &a, Any &b)
{
Any temp;

temp=a;
a=b;
b=temp;
}
int main()
{ using namespace std;

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 int swapper: \n";
swap(x,y);
cout << "Now x,y=" << x << ". " << y &