0

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;
    }




  • 2
    what is the meaning of "isn't working" ? – 463035818_is_not_an_ai May 30 '22 at 11:07
  • neither does the matrix appear on the screen nor is it filling the positions, – João Roque May 30 '22 at 11:09
  • Please [edit] your question to describe the observations, don't put relevant info into comments. Also, reduce your problem to a [mcve]. – Ulrich Eckhardt May 30 '22 at 11:13
  • Please note that `using namespace std;` followed by `typedef vector> array;` is a really, really [bad](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) idea, given [`std::array`](https://en.cppreference.com/w/cpp/container/array) exists and it's nothing like a vector of vectors. – Bob__ May 30 '22 at 14:19
  • `operator[]` should take a `std::size_t` but apart from that there is nothing wrong with it. Keep looking somewhere else for the error. – Goswin von Brederlow May 30 '22 at 15:07
  • Note: adding or multiplying matrixes of different dimensions doesn't work. The dimensions should really by part of the type so ill-formed operations will fail to compile. – Goswin von Brederlow May 30 '22 at 15:09
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 30 '22 at 15:16
  • 1
    Read [this](https://isocpp.org/wiki/faq/operator-overloading#matrix-subscript-op). – n. m. could be an AI May 31 '22 at 08:05

2 Answers2

0

Your class has a member

array m;

This nested vector is never resized. It has size 0.

In the constructor you declare a nested vector of same name

array m(L);

Whose inner vectors you resize in the constructor. Any subsequent access to the members inner vectors elements is out of bounds.

Do not declare another nested vector of same name in the constructor, but instead resize the member.

Note that array is rather misleading for a std::vector<std::vector<double>>. Moreover a nested vector is not an efficient 2d data structure. Anyhow, it is also not clear what your matriz does that you cannot already get from the std::vector<std::vector<double>>.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
0

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.

Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42