求教,C++程序中一个段错误问题,实在找不到了
//头文件
#ifndef ARRAY_H_INCLUDED
#define ARRAY_H_INCLUDED
#include <iostream>
using namespace std;
class Array
{
public:
Array();
Array( int,int );
Array( const Array &arr );
~Array();
void inputArray();
Array operator+( const Array & arr );
Array operator-( const Array & arr );
Array operator*( Array & arr );
bool operator==(const Array & arr );
bool operator!=(const Array & arr );
friend ostream & operator<<( ostream &,const Array & arr );
friend istream & operator>>( istream & in,Array & arr );
private:
int rank;//列
int row;//行
int **point;
};
#endif // ARRAY_H_INCLUDED
//.cpp 函数实现文件
#include <iostream>
#include "Array.h"
using namespace std;
Array::Array()
{
rank = 1;
row = 1;
point = new int*[1];
point[1] = new int[1];
}
Array::Array( int h,int l )
{
if( h < 0 || l < 0)
{
row = 1;
rank = 1;
}
else
{
row = h;
rank = l;
}
point = new int*[row];//申请一个长度为row的指针数组
for( int i=0;i<rank;i++ )
{
point[i] = new int[rank];//创建一个二维数组,row为行,rank为列
}
for( int j=0;j<row;j++ )
{
for( int k=0;k<rank;k++ )
{
point[j][k] = 0;
}
}
}
Array::Array( const Array &arr )
{
rank = arr.rank;
row = arr.row;
point = new int*[arr.row];
for( int i=0;i<arr.rank;i++ )
{
point[i] = new int[arr.rank];//创建一个二维数组,row为行,rank为列
}
for( int j=0;j<row;j++ )
{
for( int k=0;k<rank;k++ )
{
point[j][k] = arr.point[j][k];
}
}
}
Array::~Array()
{
for( int i=0;i<row;i++ )
{
delete[] point[row];
}
delete point;
}
istream & operator>>( istream & in,Array & arr )//重载输入
{
int temp;
cout << "请按行输入矩阵: \n";
for( int i=0;i<arr.row;i++)
{
for( int j=0;j<arr.rank;j++ )
{
in >> temp;
arr.point[i][j]= temp;
}
}
return in;
}
ostream & operator<<( ostream & out, const Array & arr )//重载输出
{
int temp;
int j = 0;
for( int i=0;i<arr.row;i++)
{
for( j=0;j<arr.rank;j++ )
{
temp = arr.point[i][j];
out << temp << " ";
}
out <<endl;
}
return out;
}
Array Array::operator+( const Array & arr )//重载加
{
cout << "两矩阵相加:\n";
Array sum( this->row,this->rank );
for( int i=0;i<=this->row;i++ )
{
for( int j=0;j<this->rank;j++ )
{
sum.point[i][j] = this->point[i][j] + arr.point[i][j];
}
}
return sum;
}
Array Array::operator-( const Array & arr )//重载减
{
cout << "两矩阵相减:\n";
// <<"结果矩阵信息: "<<this->row<<" 行 "<<this->rank<<" 列 \n";
Array sum( this->row,this->rank );
for( int i=0;i<=this->row;i++ )
{
for( int j=0;j<this->rank;j++ )
{
sum.point[i][j] = this->point[i][j] - arr.point[i][j];
}
}
return sum;
}
Array Array::operator*( Array & arr )//矩阵相乘
{
cout << "两矩阵相乘:\n"
<<"结果矩阵信息: "<<this->row<<" 行 "<<arr.rank<<" 列 \n";
Array mult( this->row,this->rank );
for( int i=0;i<this->row;i++ )
{
for( int j=0;j<this->rank;j++ )
{
for( int k=0;k<this->rank;k++ )
{
mult.point[i][j] += this->point[i][k] * arr.point[k][j];
}
}
}
return mult;
}
bool Array::operator==(const Array & arr )//判断矩阵相等
{
if(( this->row != arr.row ) || ( this->rank != arr.rank ) )
{
return false;
}
for( int i=0;i<this->row;i++ )
{
for( int j=0;j<this->rank;j++ )
{
if( this->point[i][j] != arr.point[i][j] )
{
return false;
}
}
}
return true;
}
bool Array::operator!=(const Array & arr )
{
if(( this->row != arr.row ) && ( this->rank != arr.rank ) )
{
return true;
}
for( int i=0;i<this->row;i++ )
{
for( int j=0;j<this->rank;j++ )
{
if( this->point[i][j] != arr.point[i][j] )
{
return true;
}
}
}
return false;
}
//main测试
#include <iostream>
#include "Array.h"
using namespace std;
int main()
{
Array temp(2,2);
cout <<"矩阵m1的行数2和列数2: \n";
Array m(2,2);
cin >> m;
cout <<"矩阵m2的行数2和列数2: \n";
Array m3( 2,2 );
cin >> m3;
temp=m+m3;
cout << temp <<endl;
// cout << m-m3 <<endl;
return 0;
}
Array::Array( int h,int l )
{
...
point = new int*[row];//申请一个长度为row的指针数组
// for( int i=0;i<rank;i++ )
for( int i=0;i<row;i++ )
// ------------^^^-------, I think you need row here.
{
point[i] = new int[rank];//创建一个二维数组,row为行,rank为列
}
class Array
{
public:
Array();
Array( int,int );
Array( const Array &arr );
~Array();
void inputArray();
const Array operator+( const Array & arr );
Array operator-( const Array & arr );
Array operator*( Array & arr );
bool operator==(const Array & arr );
bool operator!=(const Array & arr );
Array& operator=(const Array& other)
{
if (this != &other)
{
int** new_point = new int*[row]; //申请一个长度为row的指针数组
// 下标范围错误
// for( int i=0;i<rank;i++ )
for (int i = 0; i < row; i++)
{
new_point[i] = new int[rank]; //创建一个二维数组,row为行,rank为列
}
for (int j = 0; j < row; j++)
{
for (int k = 0; k < rank; k++)
{
new_point[j][k] = other.point[j][k];
}
}
point = new_point;
}
return *this;
}
friend ostream & operator<<( ostream &,const Array & arr );
friend istream & operator>>( istream & in,Array & arr );
private:
int rank;//列
int row;//行
int **point;
};
//.cpp 函数实现文件
Array::Array()
{
rank = 1;
row = 1;
point = new int*[1];
point[1] = new int[1];
}
Array::Array( int h,int l )
{
if( h < 0
[解决办法]
l < 0)
{
row = 1;
rank = 1;
}
else
{
row = h;
rank = l;
}
point = new int*[row];//申请一个长度为row的指针数组
// 下标范围错误
// for( int i=0;i<rank;i++ )
for( int i=0;i<row;i++ )
{
point[i] = new int[rank];//创建一个二维数组,row为行,rank为列
}
for( int j=0;j<row;j++ )
{
for( int k=0;k<rank;k++ )
{
point[j][k] = 0;
}
}
}
Array::Array( const Array &arr )
{
rank = arr.rank;
row = arr.row;
point = new int*[arr.row];
// 应该是行,你写成列了
// for( int i=0;i<arr.rank;i++ )
for( int i=0;i<arr.row;i++ )
{
point[i] = new int[arr.rank];//创建一个二维数组,row为行,rank为列
}
for( int j=0;j<row;j++ )
{
for( int k=0;k<rank;k++ )
{
point[j][k] = arr.point[j][k];
}
}
}
Array::~Array()
{
for( int i=0;i<row;i++ )
{
// 错了
// delete[] point[row];
delete[] point[i];
}
// 这个也是数组啊
// delete point;
delete[] point;
}
istream & operator>>( istream & in,Array & arr )//重载输入
{
int temp;
cout << "请按行输入矩阵: \n";
for( int i=0;i<arr.row;i++)
{
for( int j=0;j<arr.rank;j++ )
{
in >> temp;
arr.point[i][j]= temp;
}
}
return in;
}
ostream & operator<<( ostream & out, const Array & arr )//重载输出
{
int temp;
int j = 0;
for( int i=0;i<arr.row;i++)
{
for( j=0;j<arr.rank;j++ )
{
temp = arr.point[i][j];
out << temp << " ";
}
out <<endl;
}
return out;
}
const Array Array::operator+( const Array & arr )//重载加
{
cout << "两矩阵相加:\n";
Array sum( this->row,this->rank );
// 越界
// for( int i=0;i<=this->row;i++ )
for( int i=0;i<this->row;i++ )
{
for( int j=0;j<this->rank;j++ )
{
sum.point[i][j] = this->point[i][j] + arr.point[i][j];
}
}
return sum;
}
Array Array::operator-( const Array & arr )//重载减
{
cout << "两矩阵相减:\n";
// <<"结果矩阵信息: "<<this->row<<" 行 "<<this->rank<<" 列 \n";
Array sum( this->row,this->rank );
// 越界
// for( int i=0;i<=this->row;i++ )
for( int i=0;i<this->row;i++ )
{
for( int j=0;j<this->rank;j++ )
{
sum.point[i][j] = this->point[i][j] - arr.point[i][j];
}
}
return sum;
}
Array Array::operator*( Array & arr )//矩阵相乘
{
cout << "两矩阵相乘:\n"
<<"结果矩阵信息: "<<this->row<<" 行 "<<arr.rank<<" 列 \n";
Array mult( this->row,this->rank );
for( int i=0;i<this->row;i++ )
{
for( int j=0;j<this->rank;j++ )
{
for( int k=0;k<this->rank;k++ )
{
mult.point[i][j] += this->point[i][k] * arr.point[k][j];
}
}
}
return mult;
}
bool Array::operator==(const Array & arr )//判断矩阵相等
{
if(( this->row != arr.row )
[解决办法]
( this->rank != arr.rank ) )
{
return false;
}
for( int i=0;i<this->row;i++ )
{
for( int j=0;j<this->rank;j++ )
{
if( this->point[i][j] != arr.point[i][j] )
{
return false;
}
}
}
return true;
}
bool Array::operator!=(const Array & arr )
{
if(( this->row != arr.row ) && ( this->rank != arr.rank ) )
{
return true;
}
for( int i=0;i<this->row;i++ )
{
for( int j=0;j<this->rank;j++ )
{
if( this->point[i][j] != arr.point[i][j] )
{
return true;
}
}
}
return false;
}
//main测试
int main()
{
Array temp(2,2);
cout <<"矩阵m1的行数2和列数2: \n";
Array m(2,2);
cin >> m;
cout <<"矩阵m2的行数2和列数2: \n";
Array m3( 2,2 );
cin >> m3;
cout << m << endl;
cout << m3 << endl;
temp=m+m3;
cout << temp <<endl;
cout << m-m3 <<endl;
return 0;
}
Array& operator=(const Array& other)
{
assert(row == other.row && rank == other.rank && other.point != NULL);
if (this != &other)
{
for (int j = 0; j < row; j++)
{
for (int k = 0; k < rank; k++)
{
point[j][k] = other.point[j][k];
}
}
}
return *this;
}