Search code examples
c++arraysconstructorinitializer-list

array initialization requires a brace-enclosed initializer list


My class Matrix4x4 has a constructor that takes 9 values and copies them into an internal T value[4][4] member through an initializer list. However, it doesn't compile, and I'm not entirely sure why. Specifically, the error says: array initialization requires a brace-enclosed initializer list.

I'm using Visual Studio 2015.

template<typename T>
Matrix4x4<T>::Matrix4x4(
    T aa, T ba, T ca,
    T ab, T bb, T cb,
    T ac, T bc, T cc
    )
    : value({
        { aa, ba, ca,  0 },
        { ab, bb, cb,  0 },
        { ac, bc, cc,  0 },
        { 0,  0,  0,  1 }
    })
{

}

Solution

  • If you have access to a C++11 compiler, here's one solution.

    Remove the ( and ) from the initializer of value. Use:

    Matrix4x4::Matrix4x4(T aa, T ba, T ca,
                         T ab, T bb, T cb,
                         T ac, T bc, T cc)
       : value{ { aa, ba, ca,  0 },
                { ab, bb, cb,  0 },
                { ac, bc, cc,  0 },
                { 0,  0,  0,  1 } }
    {
    }