【发布时间】:2016-06-01 07:34:17
【问题描述】:
我有一个为指针分配内存的函数,如下所示:
void initializeImageBuffer(unsigned char **image, int w, int h)
{
if (*image != NULL)
delete[] *image;
*image = new unsigned char[w * h];
}
现在我想使用函数模板来概括参数类型(无符号字符/整数/双精度)。这就是我所做的:
template<typename T, int, int>
void initializeImageBuffer(T **image, int w, int h)
{
if (*image != NULL)
delete[] *image;
*image = new T[w * h];
}
但是使用如下函数会出错:
unsigned char* image;
initializeImageBuffer(&image, 200, 200);
“没有使用这些参数类型的重载函数实例。参数类型是 (unsigned char**, int, int)。”
【问题讨论】:
标签: c++ function templates pointers