分段错误重载ostream(<<<

时间:2017-03-06 21:48:14

标签: c++ ostream fault

所以我在c ++中练习编码,我试图用相关的重载操作为矩阵编写一个类(存储为数组)。

我已经定义了这个类并尝试重载<<运算符,但我当前的代码导致分段错误(我在Ubuntu中使用g ++编译)。我已经在网上看过,有类似问题的人往往总是忘记在过载功能中返回操作系统,但是我已经这样做了,所以我不知道我的问题是什么。此外,我的重载运算符在导致分段错误之前会多次运行。

非常感谢任何帮助。

这是我的代码:

#include<iostream>
#include<stdlib.h> // for c style exit
using namespace std;

class matrix
{
  // Friends
  friend ostream & operator<<(ostream &os, const matrix &mat);
  friend istream & operator>>(istream &is, matrix &mat);

private:
  double *mdata;
  int rows,columns;
public:
  // Default constructor
  matrix(){mdata=0; rows=columns=0;}
  // Parameterized constructor
  matrix(int m, int n){mdata = new double[ m*n ]; rows = m; columns = n;}
  // Copy constructor
  matrix(matrix &mat)
  // Destructor
  ~matrix(){delete[] mdata; cout<<"Destructing array."<<endl;}
  // Access functions
  int getrows() const {return rows;} // Return number of rows
  int getcols() const {return columns;} // Return number of columns
  int index(int m, int n) const // Return position in array of element (m,n)
  {
    if(m>0 && m<=rows && n>0 && n<=columns) return (n-1)+(m-1)*columns;
    else {cout<<"Error: out of range"<<endl; exit(1);}
  }
  double & operator()(int m, int n)const {return mdata[index(m,n)];}
  // Other access functions go here
  double & operator[](int i) {return mdata[i];}
  // Other functions 
  // Copy  Assignment operator
  matrix & operator=(matrix &mat);
};

// Member functions defined outside class
matrix::matrix(matrix &mat){
  rows = mat.getrows();
  columns = mat.getcols();
  for(int j = 0; j<rows*columns; j++){mdata[j] = mat[j];}
 }

matrix & matrix::operator=(matrix &mat){
  if (&mat == this) return *this;

  delete[] mdata; rows = 0; columns = 0;

  rows = mat.getrows(); columns = mat.getcols();
  if(rows>0&&columns>0){
    mdata = new double[(columns-1) + (rows-1)*columns + 1];
    for(int j = 0; j<rows*columns; j++){mdata[j] = mat[j];}
  }
  return *this;
}


// Overload insertion to output stream for matrices
ostream & operator<<(ostream &os, const matrix &mat){
  for(int j = 0;j<mat.rows;j++){
    for(int k = 0;k<mat.columns;k++){
      os << mat(j+1,k+1) << " ";
   }
    os << endl;
 }
  return os;
}

// Main program

int main(){

  // Demonstrate default constructor
  matrix a1;
  cout<<a1;

  // Parameterized constructor
  const int m(2),n(2);
  matrix a2(m,n);
  // Set values for a2 here
  a2[0] = 1; a2[1] = 2; a2[2] = 3; a2[3] = 4;
  // Print matrix a2
  cout<<a2;


  // Deep copy by assignment: define new matrix a3 then copy from a2 to a3
  matrix a3(m,n);
  cout<<a3;
  a3=a2;
  cout<<a3;
  // Modify contents of original matrix and show assigned matrix is unchanged here
  a2[0] = 5;
  cout<<a2;
  cout<<a3; //here is where segmentation fault occurs
  return 0;
}

2 个答案:

答案 0 :(得分:3)

您似乎超越了矩阵的极限。您应移除+1i ...

j
for(int j = 0;j<mat.rows;j++){
    for(int k = 0;k<mat.columns;k++){
      os << mat(j,k) << " ";
   }

当一个数组有3个元素时,这意味着你可以访问这3个元素:

array[0]
array[1]
array[2]

但不是array[3],它是数组中长度为3的第4个元素。

根据经验,当您获得segmentation fault时,您应该使用gdbvalgrind来运行您的计划。这些工具通常会为您提供非常有价值的信息,以便发现代码中内存访问错误的根本原因。

答案 1 :(得分:0)

您需要将FUNCTION设为类别矩阵的friend,因为它不能访问columnsrows,因为它们是私有成员。重载operator<<operator>>时的典型错误。永远不要忘记friend