Search code examples
c++arraysclassppm

Creating multiple Instances of Custom Class in C++


Basically what title says. I have a Custom named class Color with this constructor:

Color(component_t r, component_t g, component_t b) : r(r), g(g), b(b) {

    }

The Class "Image" i'm working on has already a protected value of: Color* buffer I am trying to fill this buffer with instances of the Color class with data from another float* array.This is my code:

    Color* buffer = new Color[width*height];

    float* r = new float[width*height];
    float* g = new float[width*height];
    float* b = new float[width*height];

    int i = 0;

    do {
        r[i] = buff[i];
        i++;
        g[i] = buff[i];
        i++;
        b[i] = buff[i];
        i++;
    } while (i < width*height);

    for (int k = 0; k < width*height; k++) {


        buffer[k] = new Color(r[k], g[k], b[k]);
    } 

However in the 'buffer[k]=new Color(r[k],g[k],b[k])' line inside the for loop its giving me 'no operator "=" matches these operands, operand types are: Color=Color*'

What am i doing wrong?


Solution

  • You have already created all the Colors here:

    Color* buffer = new Color[width*height];
    

    To just change the value of a buffer member you can do

    buffer[k] = Color(r[k], g[k], b[k]);
    

    No need to allocate a new Color, you already did that before.