From 8a024825d26aee76431e4c7dab98f114bf51dff2 Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Wed, 26 Sep 2007 14:06:32 +0000 Subject: fix bugs caused by default copy constructors being called. valgrind, you saved my life. --- doc/tutorial.cpp | 4 +--- src/internal/Matrix.h | 23 ++++++++++++----------- test/matrixops.cpp | 5 ++--- test/vectorops.cpp | 4 ++-- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/doc/tutorial.cpp b/doc/tutorial.cpp index 2d881cda2..efadc9bfa 100644 --- a/doc/tutorial.cpp +++ b/doc/tutorial.cpp @@ -17,13 +17,11 @@ int main(int, char **) cout << "Here is a 2x2 matrix m:" << endl << m << endl; cout << "Let us now build a 4x4 matrix m2 by assembling together four 2x2 blocks." << endl; - Matrix m2; // dynamic matrix with initial size 4x4 and uninitialized entries + MatrixXd m2(4,4); // dynamic matrix with initial size 4x4 and uninitialized entries // notice how we are mixing fixed-size and dynamic-size types. cout << "In the top-left block, we put the matrix m shown above." << endl; m2.block(0,1,0,1) = m; - cout << "m2 is now " << endl << m2 << endl; - cout << "m2.block(0,1,0,1) has " << m2.block(0,1,0,1).rows() << " rows" << endl; cout << "In the bottom-left block, we put the matrix m*m, which is:" << endl << m*m << endl; m2.block(2,3,0,1) = m * m; cout << "In the top-right block, we put the matrix m+m, which is:" << endl << m+m << endl; diff --git a/src/internal/Matrix.h b/src/internal/Matrix.h index a4018c29d..15042beed 100644 --- a/src/internal/Matrix.h +++ b/src/internal/Matrix.h @@ -73,29 +73,30 @@ class Matrix : public EigenBase<_Scalar, Matrix<_Scalar, _Rows, _Cols> >, public: template - Matrix& operator=(const EigenBase &other) + Matrix& operator=(const EigenBase& other) { resize(other.rows(), other.cols()); return Base::operator=(other); } - - template - Matrix& operator+=(const EigenBase &other) - { - return Base::operator+=(other); - } - template - Matrix& operator-=(const EigenBase &other) + Matrix& operator=(const Matrix& other) { - return Base::operator-=(other); + resize(other.rows(), other.cols()); + return Base::operator=(other); } - + + INHERIT_ASSIGNMENT_OPERATOR(Matrix, +=) + INHERIT_ASSIGNMENT_OPERATOR(Matrix, -=) + explicit Matrix(int rows = 1, int cols = 1) : Storage(rows, cols) {} template Matrix(const EigenBase& other) : Storage(other.rows(), other.cols()) { *this = other; } + Matrix(const Matrix& other) : Storage(other.rows(), other.cols()) + { + *this = other; + } ~Matrix() {} }; diff --git a/test/matrixops.cpp b/test/matrixops.cpp index 8acf43277..1369a72a0 100644 --- a/test/matrixops.cpp +++ b/test/matrixops.cpp @@ -45,7 +45,6 @@ template(), Matrix()); matrixOps(Matrix(), Matrix()); matrixOps(Matrix, 4,3>(), Matrix, 3,4>()); - /*matrixOps(MatrixXf(1, 1), MatrixXf(1, 3)); + matrixOps(MatrixXf(1, 1), MatrixXf(1, 3)); matrixOps(MatrixXi(2, 2), MatrixXi(2, 2)); matrixOps(MatrixXd(3, 5), MatrixXd(5, 1)); matrixOps(MatrixXcf(4, 4), MatrixXcf(4, 4)); matrixOps(MatrixXd(3, 5), Matrix()); - matrixOps(Matrix4cf(), MatrixXcf(4, 4));*/ + matrixOps(Matrix4cf(), MatrixXcf(4, 4)); } diff --git a/test/vectorops.cpp b/test/vectorops.cpp index 38beb19c2..b1bbfdbbb 100644 --- a/test/vectorops.cpp +++ b/test/vectorops.cpp @@ -54,8 +54,8 @@ void EigenTest::testVectorOps() vectorOps(Vector2i()); vectorOps(Vector3d()); vectorOps(Vector4cf()); - /*vectorOps(VectorXf(1)); + vectorOps(VectorXf(1)); vectorOps(VectorXi(2)); vectorOps(VectorXd(3)); - vectorOps(VectorXcf(4));*/ + vectorOps(VectorXcf(4)); } -- cgit v1.2.3