happy_sisyphus
happy_sisyphus

Reputation: 1827

C++ Constructor initialization list with array

What am I doing wrong here? The compiler tells me it is a syntax error.

class Color {
private:
    float rgba[4];
public:
    Color(float red, float green, float blue, float alpha=1.0):
            this->rgba[0] (red * alpha),
            this->rgba[1] (green*alpha)
            this->rgba[2] (blue*alpha)
    {

    }
};

Upvotes: 0

Views: 63

Answers (2)

Andrés Guerrero
Andrés Guerrero

Reputation: 86

You can use the asigment operator instead of parentetheses:

this->rgba[0](red * alpha) // wrong
this->rgba[1] = red * alpha // ok

Upvotes: 0

Jarod42
Jarod42

Reputation: 217085

It should be

Color(float red, float green, float blue, float alpha=1.0):
            rgba{red * alpha, green*alpha, blue*alpha, 0}
    {

    }

Upvotes: 4

Related Questions