#include <iostream>
using namespace std;
template <class T>
void create2darray(T ** &x,int numOfRows,int* colsSize)
{
try{
x = new T* [numOfRows];
for (int i = 0; i < numOfRows; i++)
{
x[i] = new T[colsSize[i]];
}
}
catch(bad_alloc){
cout << "error!" << endl;
}
}
template<class T>
T abc(T a,T b, T c)
{
return (a+ b*c);
}
int main()
{
//输出第一个模板的结果
cout << abc(1,2,3);
int* s = new int[6];
for (int i = 0; i < 6; i++)
{
s[i] = i;
}
//生成一个二维锯齿数组,有3行,6列
char ** f;
create2darray(f,3,s);
cin.get();
return 0;
}
异常一:IntelliSense: 没有与参数列表匹配的 函数模板 "make2darray" 实例
参数类型为: (char **, int)
解决:这种异常通常是参数设置不正确导致的。
异常二:无法解析的外部符号
解决:1:模板类声明应在同一个头文件中,因为在编译过程不会实例化对象,如果不在包含在同一个头文件中,无法成功编译。 2:同时包含类的定义和实现的头文件
抽象类也无法实例化