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);

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM