【发布时间】:2017-03-06 21:48:13
【问题描述】:
所以我正在练习 C++ 中的编码,并且我正在尝试为具有相关重载操作的矩阵(存储为数组)编写一个类。
我已经定义了类并试图重载
任何帮助将不胜感激。
这是我的代码:
#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;
}
【问题讨论】:
-
我建议先修复复制构造函数和赋值运算符。
-
然后用调试器运行,因为你的代码充满了琐碎的错误。
-
我已经定义了类——你没有。只需一个两行 main() 程序,这个矩阵类就可以一蹴而就。
{ matrix m(1,2); matrix m2=m;} -
我无法重现错误(在我添加编译器所需的分号之后)。这是一个最小的例子吗?
-
@user7631642 已经说过您的复制构造函数存在大问题。你没看到吗?您的赋值运算符中还有一个迫在眉睫的错误。
matrix m; matrix m2(1,2); m = m2;如果我可以用这些小例子造成破坏,也许你应该先解决这些问题。