D
D
Danil Vaskevich2021-06-04 11:24:59
C++ / C#
Danil Vaskevich, 2021-06-04 11:24:59

How to solve dynamic two dimensional array indexing error?

We need to find the smaller value in a dynamic two-dimensional array. By and large, the whole code is ready, but in the indexing places it gives the error C2676 "binary "[": "Matrix" does not define this operator or conversion to a type acceptable to the built-in operator"

Here is the code:

template<typename T, size_t rows, size_t cols>
inline int Matrix<T, rows, cols>::findSmallest(Matrix<T, rows, cols>& m)
{
    int resNum = m[0][0];
    for (size_t i = 0; i < rows; i++)
    {
        for (size_t j = 0; j < cols; j++)
        {
            if (m[i][j] < resNum)
            {
                resNum = m[i][j];
            }
        }
    }
    return resNum;
}


PS Here is the definition in the class:
int findSmallest(Matrix<T, rows, cols>& m);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
Wataru, 2021-06-04
@wataru

You work with matrix as if it were an array, but you don't have it as an array. Either redefine the [] operator, or somehow output from matrix to T**.

P
pogoreli, 2021-06-04
@pogoreli

Why do i and j both count as rows? It seems to me that there should be an out of bonds exception. I should be rows, j- columns.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question