*的运行没问题,但是*=的怎么改都无法实现
# include <iostream>
# include <vector>
# include <cmath>
# include <cstring>
#include <exception>
using namespace std;
class myException:public exception{
public:myException(string str):msg(str){
cout<<msg<<endl;
}
string msg;
};
template <class T>
class Matrix{
public:
Matrix(int r,int c){
rows=r;cols=c;
element=new T[r*c];
}
Matrix(const Matrix<T>& m){//复制构造函数
rows=m.rows;
cols=m.cols;
}
~Matrix(){
delete[]element;
}
int cols;
int rows;
T* element;
int Rows() const{ return rows; }
int Columns() const { return cols;}
T& operator()(int i, int j) const{
return element[(i-1)*cols+j-1];
}
T& num(int i, int j)const {
return element[(i-1)*cols+j-1];
}
friend ostream&operator<<(ostream& os, Matrix<T>& m){
for (int i = 1; i <=m.rows ; i++) {
for (int j = 1; j <=m.cols ; j++) {
cout<<m.element[(i-1)*m.cols+j-1]<<" ";
}
cout<<endl;
}
}
friend istream&operator>>(istream& is,Matrix<T>& m){
for (int i = 1; i <=m.rows ; i++) {
for (int j = 1; j <=m.cols ; j++) {
is>>m(i,j);
}
if(!is)throw myException("IO ERROR");
}
return is;
}
const Matrix<T> operator-(const Matrix<T> &m){//矩阵减法
if(rows!=m.rows||cols!=m.cols)throw myException("Size Mismatch!!!!");
Matrix<T> w(rows,cols);
for (int i = 0; i <rows*cols ; i++) {
w.element[i]=element[i]-m.element[i];
}
return w;
}
const Matrix<T> operator+(const Matrix<T> &m){//矩阵加法
if(rows!=m.rows||cols!=m.cols)throw myException("Size Mismatch!!!!");
Matrix<T> w(rows,cols);
for (int i = 0; i <rows*cols ; i++) {
w.element[i]=element[i]+m.element[i];
}
return w;
}
const Matrix<T>operator*(const Matrix<T> &m){//矩阵乘法
if(cols!=m.rows)throw myException("MultipleERROR");
Matrix<T>w=Matrix<T>(rows,m.cols);//结果矩阵,函数前面不能加&号!
int ct=0,cm=0,cw=0;
for (int i = 1; i <=rows ; i++) {
for (int j = 1; j <=m.cols ; j++) {
T sum=element[ct]*m.element[cm];
for (int k = 2; k <=cols ; k++) {
ct++;
cm+=m.cols;
sum+=element[ct]*m.element[cm];
}
w.element[cw++]=sum;
ct-=cols-1;
cm=j;
}
ct+=cols;
cm=0;
}
return w;
}
const void operator*=(const Matrix<T> &m){//矩阵乘法
if(cols!=m.rows)throw myException("MultipleERROR");
Matrix<T>w=Matrix<T>(rows,m.cols);//结果矩阵
int ct=0,cm=0,cw=0;
for (int i = 1; i <=rows ; i++) {
for (int j = 1; j <=m.cols ; j++) {
T sum=element[ct]*m.element[cm];
for (int k = 2; k <=cols ; k++) {
ct++;
cm+=m.cols;
sum+=element[ct]*m.element[cm];
}
w.element[cw++]=sum;
ct-=cols-1;
cm=j;
}
ct+=cols;
cm=0;
}
cout<<"乘法前:";
for (int l = 0; l < cols*rows ; l++) {
cout<<element[l]<<" ";
}cout<<endl;
cols=w.cols;
rows=w.rows;
delete[]element;
element=w.element;
cout<<"乘法后:";
for (int n = 0; n <rows*cols ; n++) {
cout<<element[n]<<" ";
}
cout<<endl;
}
};
int main(){
Matrix<int> m=Matrix<int>(2,3);
cin>>m;
cout<<m;
Matrix<int> n=Matrix<int>(3,2);
cin>>n;
cout<<n;
// Matrix<int> r=m*n;
// cout<<r;
m*=n;
cout<<m;
}
这是我运行的结果,为什么单单前两个元素是一堆数字呢?
2 个解决方案
#1
最好是调用乘法运算符,然后再赋值即可。
#2
Matrix(const Matrix<T>& m){//复制构造函数
rows=m.rows;
cols=m.cols;
}
光拷贝了rows 和 cols ,却没拷贝 element
rows=m.rows;
cols=m.cols;
}
光拷贝了rows 和 cols ,却没拷贝 element
#1
最好是调用乘法运算符,然后再赋值即可。
#2
Matrix(const Matrix<T>& m){//复制构造函数
rows=m.rows;
cols=m.cols;
}
光拷贝了rows 和 cols ,却没拷贝 element
rows=m.rows;
cols=m.cols;
}
光拷贝了rows 和 cols ,却没拷贝 element