1

The code is below

The error is "Expression must have class type" on the line: if (index >= mElements.size())

But not on the line return mElements[index]

I dont understand why, is this a glitch?

struct Matrix4x4
{
    float mElements[16];

    Matrix4x4()
    {
        memset(&this->mElements, 0, sizeof(this->mElements));
        this->mElements[0] = 1;
        this->mElements[5] = 1;
        this->mElements[10] = 1;
        this->mElements[15] = 1;
    }

    Matrix4x4(const float (&mElements)[16])
    {
        for (int i = 0; i < 16; i++)
            this->mElements[i] = mElements[i];
    }

    float& operator[](int index)
    {
        if (index >= mElements.size()) {
            printf("Array index out of bound, exiting");
            return 0.0F;
        }
        return mElements[index];
    }
}
Mich
  • 3,188
  • 4
  • 37
  • 85

2 Answers2

2

Plain arrays don't have .size(). Use std::size(mElements) from #include <iterator>, or std::extent_v<decltype(mElements)> from #include <type_traits>.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
0

mElements is a plain old array. They don't have member functions. Hence, mElements.size() is not accepted by the compiler.

With the introduction of std::array in C++11, there is less of a need to use plain arrays.

Replace

float mElements[16];

with

std::array<float, 16> mElements;

Then, you can use mElements.size().

R Sahu
  • 204,454
  • 14
  • 159
  • 270