mardi 4 février 2020

Array as map key

I'm trying to use an array as map key. I got it working but as soon as I try to integrate it in a class I'm getting the following compiler error:

In instantiation of ‘T& Matrix::operator[](std::array) [with T = double]’: 35:12: required from here 20:24: error: no match for call to ‘(std::map, double, std::less >, std::allocator, double> > >) (const std::array&)’ return matrix(index);

This is what my code looks like:

#include <map>

template <typename T>
struct Matrix{
    std::map<std::array<int,2>,T> matrix;
    int rows;
    int columns;

    Matrix()
    : rows(0),
      columns(0)
    {  }

    Matrix(int rows,int columns)
    :  rows(rows),
    columns(columns)
    { }

    T& operator[](const std::array<int,2> index){
    return matrix(index);
}

    T& operator[](const std::array<int,2>& index) const{
    return matrix(index);
}

};

int main(int argc, char *argv[])
{
    Matrix<double> M(10,10);
    double a = 10;
    M[{10,11}] = a;


    return 0;
}

Aucun commentaire:

Enregistrer un commentaire