!c++矩阵乘法有关问题
求助!!c++矩阵乘法问题
代码为
const Matrix &Matrix::operator*(const Matrix &s)
{
int i,j,n;
Matrix temp(row,column);
if(column!=s.row)
{
cout<<"不能相乘"<<endl;
}
else
{
for(i=0; i<row; i++)
for(j=0; j<row; j++)
{
for(n=0; n<column; n++)
temp.M[i][j]+=M[i][n]*s.M[n][j];
}
}
return temp;
}
在运行时 Matrix temp(row,column); 语句出现警告
为warning: reference to local variable 'temp' returned
问各位大神该如何解决??
------解决方案--------------------
返回了局部对象的引用。
const Matrix &Matrix::operator*(const Matrix &s)
改为
const Matrix Matrix::operator*(const Matrix &s)
代码为
const Matrix &Matrix::operator*(const Matrix &s)
{
int i,j,n;
Matrix temp(row,column);
if(column!=s.row)
{
cout<<"不能相乘"<<endl;
}
else
{
for(i=0; i<row; i++)
for(j=0; j<row; j++)
{
for(n=0; n<column; n++)
temp.M[i][j]+=M[i][n]*s.M[n][j];
}
}
return temp;
}
在运行时 Matrix temp(row,column); 语句出现警告
为warning: reference to local variable 'temp' returned
问各位大神该如何解决??
------解决方案--------------------
返回了局部对象的引用。
const Matrix &Matrix::operator*(const Matrix &s)
改为
const Matrix Matrix::operator*(const Matrix &s)