加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 将“指针指针”传递给模板函数

发布时间:2020-12-16 10:13:06 所属栏目:百科 来源:网络整理
导读:我有一个功能,它将内存分配给指针,如下所示: void initializeImageBuffer(unsigned char **image,int w,int h) { if (*image != NULL) delete[] *image; *image = new unsigned char[w * h]; } 现在我想使用函数模板推广参数类型(unsigned char / int / dou
我有一个功能,它将内存分配给指针,如下所示:

void initializeImageBuffer(unsigned char **image,int w,int h)
   {
        if (*image != NULL)
            delete[] *image;
        *image = new unsigned char[w * h];
   }

现在我想使用函数模板推广参数类型(unsigned char / int / double).多数民众赞成我所做的:

template<typename T,int,int>
 void initializeImageBuffer(T **image,int h)
   {
        if (*image != NULL)
            delete[] *image;
        *image = new T[w * h];
   }

但使用以下功能时出错:

unsigned char* image;
    initializeImageBuffer(&image,200,200);

“这些参数类型没有重载函数的实例.参数类型是(unsigned char **,int).”

解决方法

template<typename T,int>

在这里,您声明您的模板有三个参数,类型T和两个未命名的int.由于无法在调用站点推断出int的值,因此您需要明确地提供它们以及T,因为您只能从左到右提供模板参数:

initializeImageBuffer<unsigned char,42,42>(&image,200);

但是,你最想要的只是放弃这些内容,它们在这里绝对没用.

template<typename T>
void initializeImageBuffer(T **image,int h)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读