简体   繁体   English

矩阵类中的运算符重载函数有什么问题

[英]What is wrong in the operator overloading function in the matrix class

I had to create this class to add two matrixes, using operator overloading.我必须创建这个类来添加两个矩阵,使用运算符重载。 I don't want to use pointers in this problem because I don't understand them fully yet, I'm just trying to learn operator overloading.我不想在这个问题中使用指针,因为我还没有完全理解它们,我只是想学习运算符重载。 the error it shows is a segmentation fault (core dumped).它显示的错误是分段错误(核心转储)。

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

class Matrix {
    public : 
        vector<vector<int> >a;
        Matrix(){}
        Matrix operator+( Matrix m ){
            Matrix sum;
            for (int i=0; i<a.size(); i++ ){
                for(int j=0;j<a[i].size(); j++){
                    sum.a[i][j] = a[i][j] + m.a[i][j];
                }
            } 
            return sum;
        }
};
int main () {
   int cases,k;
   cin >> cases;
   for(k=0;k<cases;k++) {
      Matrix x;
      Matrix y;
      Matrix result;
      int n,m,i,j;
      cin >> n >> m;
      for(i=0;i<n;i++) {
         vector<int> b;
         int num;
         for(j=0;j<m;j++) {
            cin >> num;
            b.push_back(num);
         }
         x.a.push_back(b);
      }
      for(i=0;i<n;i++) {
         vector<int> b;
         int num;
         for(j=0;j<m;j++) {
            cin >> num;
            b.push_back(num);
         }
         y.a.push_back(b);
      }
      result = x+y;
      for(i=0;i<n;i++) {
         for(j=0;j<m;j++) {
            cout << result.a[i][j] << " ";
         }
         cout << endl;
      }
   }  
   return 0;
}

The vector inside your Matrix sum has zero size.矩阵sum的向量大小为零。
SO using operator[] will cause undefined behavior.所以使用operator[]会导致未定义的行为。

        Matrix sum;                                 // sum.a.size() is zero
        for (int i=0; i<a.size(); i++ ){
            for(int j=0;j<a[i].size(); j++){
                sum.a[i][j] = a[i][j] + m.a[i][j];  // broken
            }                                       // sum.a[0][0] does not exist
        } 

There are a couple of solutions.有几种解决方案。
Easiest is to resize the array so that it has the required number of rows/cols最简单的方法是调整数组大小,使其具有所需的行数/列数

You did not initialize the vector for the matrix defined on this line:您没有初始化此行上定义的矩阵的向量:

Matrix sum;

Your entire approach could use a rethink.您的整个方法可能需要重新思考。 Making the callers responsible for managing the internal representation of your matrix breaks the principle of encapsulation.让调用者负责管理矩阵的内部表示违反了封装原则。

Furthermore, using vector<vector<int>> is inefficient for non-sparse matrix representations.此外,使用vector<vector<int>>对于非稀疏矩阵表示是低效的。 You would be better off using a single block of contiguous memory.最好使用单个连续内存块。

Either way, I recommend defining a constructor for Matrix that accepts a size.无论哪种方式,我都建议为Matrix定义一个接受大小的构造函数。 Make the internal data private and define operator[] or similar to access the data将内部数据设为私有并定义operator[]或类似的来访问数据

Inside the operator you created an object of the type Matrix that has an empty vector在运算符内部,您创建了一个 Matrix 类型的对象,该对象具有一个空向量

Matrix sum;

So you may not use the subscript operator in the expression所以你不能在表达式中使用下标运算符

sum.a[i][j]

You could after creating the object sum set appropriate sizes for the vector.您可以在创建对象sum后为向量设置适当的大小。

For example例如

Matrix sum;
sum.a.assign( a.size(), std::vector<int>( a[0].size() ) );

Pay attention to that it is much better to declare the operator the following way注意以下方式声明操作符会好很多

Matrix operator+( const Matrix &m ) const;

because neither operand of the operator is changed in the operator.因为运算符的操作数都没有在运算符中改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM