Please help me with a brain fart on Array class

I must be losing it, but I don’t get it.

If I can do this sort of local declaration:

const Array<Colour> swatchColours = {
    Colours::red,
    Colours::green,
    Colours::blue };

Why can’t I do this? It keeps telling me “No matching member function for call to ‘add’”:

const Array<Colour> swatchColours;
swatchColours.add(Colours::red);      // No matching member function for call to 'add'
swatchColours.add(Colours::green);    // No matching member function for call to 'add'
swatchColours.add(Colours::blue);     // No matching member function for call to 'add'

It doesn’t work with an OwnedArray either…

Because you declare the Array as const, you can’t use non-const methods of it. “Add” can’t be non-const because it needs to mutate the object.

3 Likes

Aha! Thank you! Knew it was something stupid. :slight_smile: