Search code examples
c++matrixoperator-keyword

operator [][] matrix c++


Im trying to create an operator that gives me the value for the position i,j of a certain matrix and another one that "fills" the matrix in the given positions, ive tried to put the operator that you can see in the header but it isnt working, the problem might be somewhere else but i think this it the main issue:


    int main()
    {
        matriz a(3,3);
        matrizQuad b(3);
    
        for(size_t i=0;i<3;++i)
            for(size_t j=0;j<3;++j){
                a[i][j]=1;
                b[i][j]=2;
            }
        matriz c=a+b;
        cout << "a+b:\n" << c << "\n";
        cout << "traco: " << b.traco() << "\n";
    
        return 0;
    } ``` 
    
    Header:
    
    #ifndef MATRIZES_H
    #define MATRIZES_H
    #include <iostream>
    #include <vector>
    #include <ostream>
    #include <sstream>
    using namespace std;
    
    typedef vector<vector<double>> array;
    
    class matriz
    {
    protected:
        int iLargura, iComprimento;
        array m;
    public:
        matriz(int L, int C): iLargura (L), iComprimento (C)
        {
            array m(L);
            for (int i = 0; i<L;i++)
                m[i].resize(C);
        };
    
        int nL() const {return iLargura;}
        int nC() const {return iComprimento;}
    
        vector<double>& operator[] (int i) {return  m[i];}
        const vector<double>& operator[] (int i) const {return  m[i];}
    
    };
    
    class matrizQuad: public matriz
    {
    public:
        matrizQuad (int T): matriz(T,T) {}
        double traco();
    };
    
    matriz operator+(const matriz& m1, const matriz& m2);
    ostream& operator<<(ostream& output, const matriz& m1);
    
    
    #endif // MATRIZES_H
    
    Body: 
    
    #include "matriz.h"
    
    
    double matrizQuad::traco()
    {
        double dSoma = 0;
        for (int i = 0;i<iLargura; i++)
            for (int j = 0;i<iLargura; i++)
                if (i==j)
                    dSoma = dSoma + m[i][j];
        return dSoma;
    }
    
    matriz operator+(const matriz& m1, const matriz& m2)
    {
        int C1 = m1.nC();
        int L1 = m1.nL();
        matriz m(C1,L1);
    
        for (int i = 0;i<C1; i++)
            for (int j = 0;i<L1; i++)
                m[i][j] = m1[i][j] + m2[i][j];
        return m;
    }
    
    ostream& operator<<(ostream& output, const matriz& m1)
    {
        for(int i = 0; i< m1.nL();i++)
        {
            for (int j=0;j<m1.nC();j++)
                output << m1[i][j] << " ";
            output << "\n";
        }
    
        return output;
    }





Solution

  • Fixing the array resize as 463035818_is_not_a_number suggested gives you a somewhat working matrix.

        matriz(int L, int C): iLargura (L), iComprimento (C)
        {
            m.resize(L);
            for (int i = 0; i<L;i++)
                m[i].resize(C);
        };
    

    If you also print the matrixes a and b you get:

    a:
    1 1 1 
    1 1 1 
    1 1 1 
    
    b:
    2 2 2 
    2 2 2 
    2 2 2 
    
    a+b:
    3 0 0 
    3 0 0 
    3 0 0 
    

    So setting and printing matrixes works just fine. The error is in the operator +:

    matriz operator+(const matriz& m1, const matriz& m2)
    {
        int C1 = m1.nC();
        int L1 = m1.nL();
        matriz m(C1,L1);
    
        for (int i = 0;i<C1; i++)
            for (int j = 0;i<L1; i++)
                m[i][j] = m1[i][j] + m2[i][j];
        return m;
    }
    

    specifically:

            for (int j = 0;i<L1; i++)
    

    I guess you copy&pasted the previous loop for i and forgot to rename all the variables.