注册 登录
编程论坛 C++教室

【请教】函数模板,这个程序怎么提示变量未声明呢》》??

bxxue 发布于 2013-10-09 22:40, 449 次点击
#include "stdafx.h"
#include <iostream>
using namespace std;
template <typename T>
void Swap (T &a,T &b);

int _tmain(int argc, _TCHAR* argv[])
{
    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";

    system ("pause");

    return 0;
}
void Swap (T &a,T &b)
{
    T temp;
    temp = a;
    a = b;
    b = temp;
}
2 回复
#2
blueskiner2013-10-09 23:30
template <typename T>
void Swap (T &a,T &b)
{
    T temp;
    temp = a;
    a = b;
    b = temp;
}
#3
木头心2013-10-10 12:20
template <typename T>
void Swap (T &a,T &b)
是一个函数头
1