0
votes
struct Vertex
{
    float Position[3];
    float Color[4];
    float TextCoords[2];
    float TexId;
};

static std::array<Vertex, 4> CreateQuad(float x, float y) {
    Vertex v;
    v.Position = { 0.0f,0.0f,0.0f };
}

this gives me error that the v must modifiable ivalue. and and the too many initializer value.

2
Vertex v{}; would achieve the goalM.M

2 Answers

1
votes

You can't assign a plain array with an initializer list, but you can assign a std::array that way:

struct Vertex
{
    std::array<float, 3> Position;
    std::array<float, 4> Color;
    std::array<float, 2> TextCoords;
    float TexId;
};

 static std::array<Vertex, 4> CreateQuad(float x, float y) {
    std::array<Vertex, 4> v;    
    v[0].Position = { 0.0f, 0.0f, 0.0f };
    // fill the rest of v...
    return v;
}
1
votes

You cannot assign an array to an initializer list. That's why you get an error:

error: assigning to an array from an initializer list

However, you can pass std::initializer_list to your constructor and copy its members to an ordinary array like:

Vertex(std::initializer_list<float> const& position) {
    std::copy(position.begin(), position.end(), Position);
}

and then initialize like Vertex v({ 0.0f,0.0f,0.0f });.

However, I would suggest you using std::array instead of an ordinary array.