invalid conversion from `const void*' to `void*'

在编译一个工程时出错,使用memcpy函数处报错 invalid conversion from `const void*' to `void*'

void image2mat(const image<uchar>* I, cv::Mat& img){
    int width = I->width();
    int height = I->height();
    img.create(height, width, CV_8UC1);
    memcpy(img.datastart, (char *)imPtr(I, 0, 0), width * height * sizeof(uchar));
}

原来memcpy()函数:

void* memcpy(void*, const void*, size_t)’

第一个参数是void * ( 非const指针 ),而opencv3.2中 cv::Mat.datastart 返回的是const指针,在这里即红色部分,img.datastart返回的是const char*,(不清楚3.0之前的版本是不是返回的非const指针)

解决方法:改为

memcpy(const_cast<uchar *>(img.datastart), (char *)imPtr(I, 0, 0), width * height * sizeof(uchar));

 或者用常见的强制类型转换方法:

(uchar*)img.datastart

 

const_cast功能:

const类型强制转化为非const类型

const_cast<new type>(expression)

例:

const int a = 5;
int* b = &a ;//error : const变量值不能更改,普通指针不能指向常量
const int *c = &a;//正确

使用const_cast强制转换为非const:

const int a = 5;
int* b = const_cast<int *>(&a);

 

转载于:https://www.cnblogs.com/voyagee/p/7479575.html

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值