【问题标题】:Why am I getting segmentation fault/how do I overload an operator?为什么会出现分段错误/如何重载运算符?
【发布时间】:2015-11-24 05:20:29
【问题描述】:

这是 HackerRank (Introduction C++) 上的一个问题。我得到了 main()。我正在尝试为由向量> 表示的矩阵编写一个类。我需要重载 + 以使其添加我的矩阵对象。测试将输入四行:第一行将说明预期有多少个测试用例,第二行将指定维度(第一个数字是行,第二个数字是列),第三行将给出第一个矩阵(前 m 个数字,用空格分隔, 将进入第一行......然后接下来的 m 个数字将进入第二行......等等),第四行将给出第二个矩阵。

当我运行第一个测试用例时,我遇到了分段错误。当我消除了运算符定义中的 int 声明和 for 循环,并简单地让它返回 matrixprime 时,我显然得到了矩阵加法的错误答案,但它至少输出没有错误。 之前,为了访问矩阵的元素,我使用了“matrix.a.at(i).at(j)”而不是“matrix.a[i][j]”,并得到了向量的 out_of_range 错误.我觉得这些问题在某种程度上与我的运算符定义中的临时矩阵对象(如果这是正确的称呼)有关,但不明白到底出了什么问题。任何帮助将不胜感激!

enter code here

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

class Matrix {
public:
vector<vector<int>> a;
Matrix operator+ (const Matrix&);
};

Matrix Matrix::operator+ (const Matrix& matrixprime) {
    Matrix matrix;
    int n = matrix.a.size(); 
    int m = matrix.a[0].size();
    for (int i=0; i<n; i++) {
            for (int j=0; j<m; j++) {
            matrix.a[i][j] = matrix.a[i][j] + matrixprime.a[i][j];  
        };
    };
    return matrix;  
};

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

【问题讨论】:

    标签: c++ vector segmentation-fault


    【解决方案1】:

    使用时:

    Matrix matrix;
    

    编译器使用编译器创建的默认构造函数。它使用其默认构造函数初始化成员a,该构造函数是一个空向量。

    您需要创建一个具有正确数量元素的向量,然后才能使用at()oprator[]

    使用空向量,行

    int m = matrix.a[0].size();
    

    是未定义行为的原因。

    你需要一些类似的东西:

    Matrix Matrix::operator+ (const Matrix& matrixprime)
    {
       // Initialize the matrix to be returned to be the same
       // as the LHS of the operator.
       Matrix matrix;
       matrix.a = this->a;
    
       // Now increment the elements of the matrix to be returned
       // by the elements of the RHS of the operator.
       int n = matrix.a.size(); 
       for (int i=0; i<n; i++)
       {
          int m = matrix.a[i].size();
          for (int j=0; j<m; j++)
          {
             matrix.a[i][j] += matrixprime.a[i][j];  
          }
       }
    
       return matrix;  
    }
    

    【讨论】:

    • 非常感谢!我得到了它的工作,我已经看过很多“this”运算符,但并不真正了解它是如何工作的。首先,指针的概念对我来说有点新。而且,您介意解释一下“this”使用的语法吗?编译器在读取“this”时在做什么?
    • @Yang-YangFeng,看看en.cppreference.com/w/cpp/language/this。希望它能回答您关于this 的问题。
    【解决方案2】:

    错误是在您的加号运算符重叠函数上,matrix var 未初始化,另一方面您应该使用类似 matrix.a[i][j] = this-&gt;a[i][j] + matrixprime.a[i][j]; 的东西

    【讨论】:

    • 对你的代码做了一点改动,我认为这对于工作代码来说已经足够了;)最好的问候
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    相关资源
    最近更新 更多