Error C2664: cannot convert argument 1 from 'imaging::component_t *' to 'const imaging::component_t *&'

1.5k Views Asked by At

I have the error I mentioned in the title on this part of my code.

component_t *buffer = new component_t[3 * width*height];
component_t getRawDataPtr();

...
    for (unsigned int i = 0; i < width*height * 3; i = i + 3) {
        file.read((char *)cR, sizeof(char));
        file.read((char *)cG, sizeof(char));
        file.read((char *)cB, sizeof(char));
        buffer[i] = cR / 255.0f;
        buffer[i + 1] = cG / 255.0f;
        buffer[i + 2] = cB / 255.0f;
    }
    file.close();

    image->setData(buffer);

...

void Image::setData(const component_t * & data_ptr) {
    if (height == 0 || width == 0 || buffer == nullptr)
        return;
    for (unsigned int i = 0; i < height*width * 3; i++)
        buffer[i] = data_ptr[i];
}

I tried image->setData(*buffer) or image->setData(&buffer) but that didn't work either. If anyone knows how to fix this I'd appreciate it. Thanks in advance.

2

There are 2 best solutions below

1
marcinj On

You can either change:

void Image::setData(const component_t * & data_ptr) {

to:

void Image::setData(const component_t * data_ptr) {

or:

image->setData(buffer);

to

const component_t *cbuffer = buffer;
image->setData(cbuffer);
0
Olaf Dietsche On

You try to assign a const pointer to a non-const pointer

buffer[i] = data_ptr[i];

This is not allowed, because this would violate the const promise on data_ptr.