ニューラル ネットワーク プログラムとオーバーロードされた算術演算子用のカスタム マトリックス ライブラリがあります。クラス宣言は次のとおりです。
class Matrix{
public:
int m;
int n;
double **mat;
Matrix(int,int);
Matrix(int);
Matrix(const Matrix& that):mat(that.mat),m(that.m),n(that.n)
{
mat = new double*[m];
for(int i = 0;i<m;i++)mat[i] = new double[n];
};
~Matrix();
friend istream& operator>>(istream &in, Matrix &c);
friend ostream& operator<<(ostream &out, Matrix &c);
Matrix operator+(const Matrix& other);
};
+ 操作の関数定義は次のとおりです。
Matrix Matrix::operator+(const Matrix& other)
{
Matrix c(m,n);
for(int i=0;i<m;i++)
{
for(int j = 0; j<n;j++)
c.mat[i][j] = mat[i][j] + other.mat[i][j];
}
return c;
}
私はあらゆる方法でそれを実装しようとしましたが、エラーは同じです...ここにインスタンスがあります
Matrix x(m,n); //m and n are known
x = a+b; // a and b are also m by n matrices
ブレークポイントを使用してコードをデバッグしましたが、ここにエラーがあります... 演算子関数のローカル行列 'c' は、返される前に破棄されるため、x に割り当てられているのはガベージ ポインターです。
私に何か提案してください...