操作员[] []矩阵C++

发布于 2025-02-02 19:15:57 字数 2585 浏览 4 评论 0原文

我试图创建一个运算符,使我为某个矩阵的i,j的位置和另一个“填充”矩阵的位置的位置的值,IVE试图将操作员放置在标题中,但是它无法正常工作,问题可能在其他地方,但我认为这是主要问题:


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




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




如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月棠 2025-02-09 19:15:57

将数组调整大小固定为463035818_is_not_a_number建议您可以使您有些工作。

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

如果您还打印了矩阵A和B,则可以得到:

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 

因此设置和打印矩阵效果很好。该错误是在操作员 +中:

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

具体来说:

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

我想您复制了i的上一个循环,忘了重命名所有变量。

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.

野稚 2025-02-09 19:15:57

您的班级有一个成员,

array m;

这个嵌套向量从未进行调整。它具有大小0

在构造函数中,您声明了一个相同名称的嵌套向量

array m(L);

,其内部向量您在构造函数中调整了大小。随后对成员内部向量元素的任何后续访问都超出了界限。

不要在构造函数中声明另一个同名的嵌套向量,而是调整成员的大小。

请注意,数组std :: vector&lt; std :: vector&lt; double&gt;&gt;&gt;的误解。而且,嵌套向量不是有效的2D数据结构。无论如何,也不清楚您的matriz做什么,您无法从std :: vector&lt; std :: vector&lt; double&gt;&gt;&gt; 。

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>>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文