函数模版要注意的地方见注释,代码如下:
// test.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
/*
* 函数模版的格式为: template<class T> 函数名(函数参数);
* 模版函数的实现要和声明放在一起,如果不放在一起,在实现的时候要重新声明template<class T>
*/
template<class T>
void add(T a, T b)
{
T temp = 0;
temp = a + b;
cout << temp << endl;
}
template<class T>
void sub(T a, T b)
{
T temp = 0;
temp = a-b;
cout << temp << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
int num1 = 1;
int num2 = 2;
double db1 = 22.33;
double db2 = 33.55;
add(num1, num2);
add(db1, db2);
sub(db1, db2);
system("pause");
return 0;
}// test.cpp : 定义控制台应用程序的入