Destructor not called and issue with program exiting abnormally - c++

I have the following code for defining a class Matrix. The definitions in the header file are as follows:
#ifndef MATRIX_H
#define MATRIX_H
/* General matrix class */
class Matrix
{
public:
Matrix(int m, int n, double ini);
virtual ~Matrix();
Matrix(const Matrix& other); //copy ctor
Matrix& operator=(const Matrix& other); //assignment operator;
double operator()(int i, int j) const; //access element in the matrix
double& operator() (int i, int j); //set element in the matrix
friend Matrix operator+(const Matrix& mat1, const Matrix& mat2); //Matrix addition
friend Matrix operator+(const Matrix& mat1, double a); //scaler multiplication
int dimension() const {return rows*cols;} //getter method for dimension
protected:
private:
int rows; //number of rows in the matrix
int cols; //number of cols in the matrix
double* d; //pointer to the representation of the matrix
};
The implementation of the parts relevant to the question are shown below.
#include "Matrix.h"
#include<iostream>
Matrix::Matrix(int m, int n, double ini):rows{m},cols{n},d{new double[m*n]}
{
//ctor
double* p = d;
for(int i=0;i<rows*cols;i++)
{
*p++ = ini;
}
}
Matrix::~Matrix()
{
//dtor
delete []d;
}
Matrix& Matrix::operator=(const Matrix& rhs)
{
if (this == &rhs) return *this; // handle self assignment
if (rows*cols<=rhs.rows*rhs.cols)
{
delete []d;
d = new double[rhs.rows*rhs.cols];
rows = rhs.rows;
cols = rhs.cols;
for(int i=0;i<rows*cols;i++)
{
d[i] = rhs.d[i];
}
}
//assignment operator
return *this;
}
double Matrix::operator()(int i, int j) const
{
return d[rows*i + j];
}
double& Matrix::operator()(int i, int j)
{
return d[rows*i+j];
}
Now, I have a simple test application that creates a matrix, assigns values to the elements in the matrix, as well reads the value of an element (given a row and a column number).
#include <iostream>
#include "Matrix.h"
using namespace std;
int main()
{
int i,j;
Matrix A(3,2,0.0);
cout<<A.dimension()<<endl;
// assign values to matrix elements
for (i=0;i<3;i++)
{
for (j=0;j<2;j++) A(i,j) = 0.1*i*j;
}
// access matrix elements
double sum = 0.0;
for (i=0;i<3;i++) {
for (j=0;j<2;j++) sum += A(i,j); }
cout << "The sum of the matrix elements is ";
cout << sum << endl;
return 0;
}
My issue is that while everything compiles without problems, when run, the main function freezes -- the "sum" above is computed though. Was wondering if this is due to the destructor not being called or being unable to be called. Much appreciated if anyone has any ideas.

I think your code are wrong in operator()
double Matrix::operator()(int i, int j) const
{
return d[cols*i + j];
}
double& Matrix::operator()(int i, int j)
{
return d[cols*i+j];
}
And you overflow the array d[]

Related

No match for operator*=

I've been working on a project in school that has been giving me some compiling issues and was wondering if I could have a second set of eyes look at what I am doing incorrectly, the error I keep getting is in line 47 of my test file saying there is not a match for operator *= which is confusing the heck out of me. I'll include my .h (header) my .cc (implementation) and my test file.
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
using namespace std;
class Matrix {
public:
// Construction
Matrix(int, int, double);
// Copy construction
Matrix(const Matrix& m);
// Destructor
~Matrix();
// Index operators
const double& operator()(int i, int j) const; //to work on const objects
double& operator()(int i, int j);
// Copy assignment operator
Matrix& operator=(const Matrix& m);
// Compound arithmetic operators
Matrix& operator+=(const Matrix& x);
Matrix& operator-=(const Matrix& x);
Matrix& operator*=(const Matrix& m);
Matrix& operator*=(double d); // scalar multiplication
// Output
void print(ostream& sout) const; //display the matrix onto output stream
// sout neatly
int ncols() const; //return the number of columns
int nrows() const; // return the number of rows
private:
int rows; // number of rows
int cols; // number of columns
double **element; //dynamic array to hold data
};
// Arithmetic operators are not members
Matrix operator+(const Matrix& l, const Matrix&r); // return l+r
Matrix operator-(const Matrix& l, const Matrix&r); // return l-r
Matrix operator*(const Matrix& l, const Matrix&r); // return l*r
Matrix operator*(double d, const Matrix& r); // return d*l
Matrix operator*(const Matrix& m, double d); // return l*d
// Overloaded stream insertion operator
ostream& operator<<(ostream& out, const Matrix& x);
#endif
Now this is my .cc implementation file
#include<iostream>
#include<cstdlib>
#include<stdexcept>
#include<cassert>
#include<iomanip>
#include"Matrix.h"
using namespace std;
Matrix::Matrix(int r=0, int c=0, double d=0.0) // default constructor to initialize everything to zero
{
rows=r;
cols=c;
element=new double*[r];
for(int i=0;i<r;++i){
element[i]=new double[c];}
for(int i=0; i<r; ++i){
for(int j=0; j<c; ++j){
element[i][j]=d;
}
}
}
Matrix::Matrix(const Matrix& m)
{
rows=m.rows;
cols=m.cols;
element=new double*[rows];
for(int i=0;i<rows;++i)
element[i]= new double[cols];
for(int i=0; i<rows;++i)
for(int j=0; j<cols;++j)
element[i][j]=m.element[i][j];
}
Matrix::~Matrix()
{
for (int i=0;i<rows;i++){
delete [] element[i];
}
delete [] element;
}
const double& Matrix::operator() (int i, int j) const
{
return element[i][j];
}
double& Matrix::operator()(int i, int j)
{
return element[i][j];
}
Matrix& Matrix::operator=(const Matrix& m)
{
if(this==&m){
return *this;
}
else if(rows !=m.rows || cols !=m.cols)
{
delete [] element;
rows=m.rows;
cols=m.cols;
element= new double*[rows];
for(int i=0; i<rows;i++){
element[i]=new double[cols];
}
}
return *this;}
Matrix& Matrix::operator+=(const Matrix& x)
{
for(int i=0; i<rows; ++i){
for(int j=0; j<cols; ++j){
element[i][j]+=x.element[i][j];
}
}
return *this;
}
Matrix& Matrix::operator-=(const Matrix& x)
{
for(int i=0; i<rows; ++i){
for(int j=0; j<cols; ++j){
element[i][j]-=x.element[i][j];
}
}
return *this;
}
Matrix& Matrix::operator*=(const Matrix& m)
{
Matrix temp( rows, m.cols);
for(int i=0; i<temp.rows; ++i){
for(int j=0; j<temp.cols; ++j){
for(int k=0; k<cols; ++k){
temp.element[i][j]+=(element[i][k]* m.element[k][j]);
}
}
}
return(*this=temp);
}
Matrix& Matrix::operator*=(double d)
{
for(int i=0; i<rows; ++i){
for(int j=0; j<cols; ++j){
element[i][j] *= d;
}
}
return *this;
}
void Matrix::print(ostream& sout) const
{
for (int i = 0; i < rows; ++i) {
sout << element[i][0];
for (int j = 1; j < cols; ++j) {
sout << " " << element[i][j];
}
sout << endl;
}
}
int Matrix::ncols() const
{
return cols;
}
int Matrix::nrows() const
{
return rows;
}
Matrix operator+(const Matrix& l, const Matrix&r)
{
Matrix temp(l);
return(temp+=r);
}
Matrix operator-(const Matrix& l, const Matrix&r)
{
Matrix temp(l);
return (temp -=r);
}
Matrix operator*(const Matrix& l, const Matrix&r)
{
Matrix temp(l);
return (temp*=r);
}
Matrix operator*(double d, const Matrix&r)
{
return(r*d);
}
Matrix operator*(const Matrix& m, double d)
{
Matrix temp(m);
return(temp*=d);
}
ostream& operator<<(ostream& out, const Matrix& x)
{
x.print(out);
return out;
}
Now this is my test file which where I try to compile gives me the error:
test_matrix.cc:47:13: error: no match for ‘operator*=’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘int’)
cout<
#include<iostream>
#include"Matrix.h"
#include"Matrix.cc"
using namespace std;
int main()
{
Matrix m1(3,3,2.0);
Matrix m2(3,3,3.0);
cout<<"****Testing readMatrixData, and overoladed <<"<<endl;
cout<<"Matrix m1 is:"<<endl;
cout<<m1;
cout<<"Matrix m2 is:"<<endl;
cout<<m2;
cout<<"****Testing index subscript operator"<<endl;
cout<<"m1(2,1) is "<<m1(2,1)<<endl;
cout<<"Now Set m1(2,1) to -19"<<endl;
m1(2,1)=-19;
cout<<"m1 has "<<m1.nrows()<<"rows."<<endl;
cout<<"m1 has "<<m1.ncols()<<"columns."<<endl;
cout<<"m1 is now:"<<endl;
cout<<m1;
Matrix m3=m1+m2;
cout<<"Matrix m3 = m1 + m2:"<<endl;
cout<<m3;
Matrix m4=m1-m2;
cout<<"Matrix m4 = m1 - m2:"<<endl;
cout<<m4;
Matrix m5=m3*m4;
cout<<"Matrix m5 = m3 * m4:"<<endl;
cout<<m5;
Matrix m6=m3;
cout<<"After m6 =m3 matrix m6 is:"<<endl;
cout<<m6;
cout<<"****Testing scalar multiplication"<<endl;
cout<<"m3 *= 2 and m3 is now:"<<endl;
cout<<m3 *= 2.0;
Matrix m7 = m3 * 2.0;
cout<<"****Testing matrix times a constant"<<endl;
cout<<"m7 = m3 * 2 and m7 is:"<<endl;
cout<<m7;
Matrix m8 = 2.0 * m3;
cout<<"****Testing a scalar times a matrix"<<endl;
cout<<"m8 = 2 * m3 and m8 is:"<<endl;
cout<<m8;
Matrix m9(2,2,2.0);
cout<<"Matrix m9 is"<<endl;
cout<<m9;
Matrix m10(2,2,1.0);
cout<<"Matrix m10 is"<<endl;
cout<<m10;
cout<<"Testing *= with m9 *= m10. m9 is:"<<endl;
m9= m9*=m10;
cout<<m9;
cout<<"Testing += with m9 += m10. m9 is:"<<endl;
m9=m9+=m10;
cout<<"Testing -= with m9 -= m10. m9 is:"<<endl;
m9=m9-=m10;
return 0;
}
error: no match for ‘operator*=’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘int’)
Operator *= has lower precedence than operator <<, hence, cout << m3 *= 2.0 is evaluated as (cout << m3) *= 2.0, which produces the error.
You need to use parentheses to get the required order of evaluation. E.g.:
cout << (m3 *= 2.0);

C++, overload * for matrix multiplication

I'm having a great deal of trouble trying to overload the multiplication operator * for matrix multiplication. I've defined a matrix class
#ifndef MMATRIX_H
#define MMATRIX_H
#include <vector>
#include <cmath>
// Class that represents a mathematical matrix
class MMatrix
{
public:
// constructors
MMatrix() : nRows(0), nCols(0) {}
MMatrix(int n, int m, double x = 0) : nRows(n), nCols(m), A(n * m, x)
{}
// set all matrix entries equal to a double
MMatrix &operator=(double x)
{
for (int i = 0; i < nRows * nCols; i++)
A[i] = x;
return *this;
}
// access element, indexed by (row, column) [rvalue]
double operator()(int i, int j) const
{
return A[j + i * nCols];
}
// access element, indexed by (row, column) [lvalue]
double &operator()(int i, int j)
{
return A[j + i * nCols];
}
// size of matrix
int Rows() const { return nRows; }
int Cols() const { return nCols; }
// operator overload for matrix * vector. Definition (prototype) of member class
MVector operator*(const MMatrix& A);
private:
unsigned int nRows, nCols;
std::vector<double> A;
};
#endif
And here is my attempted operator overload
inline MMatrix operator*(const MMatrix& A, const MMatrix& B)
{
MMatrix m(A), c(m.Rows(),m.Cols(),0.0);
for (int i=0; i<m.Rows(); i++)
{
for (int j=0; j<m.Cols(); j++)
{
for (int k=0; k<m.Cols(); k++)
{
c(i,j)+=m(i,k)*B(k,j);
}
}
}
return c;
}
I'm sure that there is nothing wrong with the actual multiplication of elements.
The error that I get is from my main .cpp file where I have tried to multiply two matrices together C=A*B; and I get this error,
error: no match for 'operator=' (operand types are 'MMatrix' and 'MVector')
There are 2 ways to overload operator*:
MMatrix MMatrix::operator*(MMatrix); //or const& or whatever you like
MMatrix operator*(MMatrix, MMatrix);
These are both valid, but different with slightly different semantics.
For your definition to match your declaration change the definition to:
MMatrix MMatrix::operator*(const MMatrix & A)
{
//The two matrices to multiple are (*this) and A
MMatrix c(Rows(),A.Cols(),0.0);
for (int i=0; i < Rows(); i++)
{
for (int j=0; j < A.Cols(); j++)
{
for (int k=0; k < Cols(); k++)
{
c(i,j) += (*this)(i,k)*A(k,j);
}
}
}
return c;
}
As for the error you're seeing, it seems in your class you declared the operator to take a matrix and return a vector. You probably meant to return a matrix instead. The error is telling you that you can't assign a MVector to a MMatrix.
I believe, you need to define copy constructor and copy assignment:
MMatrix(const MMatrix& other);
MMatrix& operator=(const MMatrix& other);
Move constructor and assignment wouldn't heart either:
MMatrix(MMatrix&& other);
MMatrix& operator=(MMatrix&& other);
Same goes to your MVector.

How to correctly overload + operator

I am trying to define a simple class to work with 2d matrices, called Matrix2D01, and having trouble with the + operator.
I have the += operator which is working fine,
Matrix2D01& Matrix2D01::operator+=(Matrix2D01& mat2) {
int i,j;
for (i=0;i<M;i++)
for (j=0;j<N;j++) mat[i][j]+=mat2[i][j];
return *this;
}
The + operator is defined:
Matrix2D01 Matrix2D01::operator+(Matrix2D01& mat2) {
Matrix2D01 result(1,1);
result=*this;
result+=mat2;
return result;
}
When i try to use the + operator, for example by
mat3=mat1+mat2;
the compiler gives an error:
../MatrixGames.cpp:17:12: error: no match for ‘operator=’ in ‘mat3 = Matrix2D01::operator+(Matrix2D01&)((* & mat2))’
If anyone can tell me what I'm doing wrong it will be greatly appreciated.
Thanks.
I also have a = operator defined,
Matrix2D01& Matrix2D01::operator=(Matrix2D01& mat2) {
if (this==&mat2) return *this;
int i,j;
for (i=0;i<M;i++) {
delete [] mat[i];
}
delete [] mat;
M=mat2.Dimensions()[0];
N=mat2.Dimensions()[1];
mat = new double* [M];
for (i=0;i<M;i++) {
mat[i]=new double [N];
}
for (i=0;i<M;i++)
for (j=0;j<M;j++)
mat[i][j]=mat2[i][j];
return *this;
}
here is a complete test case:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
using namespace std;
class Matrix2D01 {
protected:
int D;
double **mat;
int M,N;
public:
Matrix2D01(int m,int n);
Matrix2D01(int m,int n,double d);
~Matrix2D01();
vector <int> Dimensions() {vector <int> d(D,M); d[1]=N; return d;}
double* operator[](int i) {return mat[i];}
Matrix2D01& operator=(Matrix2D01& mat2);
Matrix2D01& operator+=(Matrix2D01& mat2);
Matrix2D01 operator+(Matrix2D01& mat2);
};
Matrix2D01::Matrix2D01(int m, int n) {
int i,j;
D=2;
M=m;
N=n;
mat = new double* [M];
for (i=0;i<M;i++) {
mat[i]=new double [N];
}
for (i=0;i<M;i++)
for (j=0;j<M;j++)
mat[i][j]=0;
}
Matrix2D01::Matrix2D01(int m, int n,double d) {
int i,j;
D=2;
M=m;
N=n;
mat = new double* [M];
for (i=0;i<M;i++) {
mat[i]=new double [N];
}
for (i=0;i<M;i++)
for (j=0;j<M;j++)
mat[i][j]=d;
}
Matrix2D01::~Matrix2D01() {
int i;
for (i=0;i<M;i++) {
delete [] mat[i];
}
delete [] mat;
}
Matrix2D01& Matrix2D01::operator=(Matrix2D01& mat2) {
if (this==&mat2) return *this;
int i,j;
for (i=0;i<M;i++) {
delete [] mat[i];
}
delete [] mat;
M=mat2.Dimensions()[0];
N=mat2.Dimensions()[1];
mat = new double* [M];
for (i=0;i<M;i++) {
mat[i]=new double [N];
}
for (i=0;i<M;i++)
for (j=0;j<M;j++)
mat[i][j]=mat2[i][j];
return *this;
}
Matrix2D01& Matrix2D01::operator+=(Matrix2D01& mat2) {
int i,j,M2,N2;
M2=mat2.Dimensions()[0];
N2=mat2.Dimensions()[1];
if ((M!=M2)||(N!=N2)) {
cout<<"error: attempted to add non-matching matrices";
return *this;
}
for (i=0;i<M;i++)
for (j=0;j<N;j++) mat[i][j]+=mat2[i][j];
return *this;
}
Matrix2D01 Matrix2D01::operator+(Matrix2D01& mat2) {
Matrix2D01 result(1,1);
result=*this;
result+=mat2;
return result;
}
int main() {
Matrix2D01 mat1(2,2,1);
Matrix2D01 mat2(2,2,2);
Matrix2D01 mat3(2,2,4);
mat3+=mat1;
mat3=mat1+mat2;
return 1;
}
Herb Sutter advocates the following canonical way to overload operator + (please read GotW, it is really well written and interesting):
Don't include operator + as a member function, instead make it a free function.
Pass one object by value and the other by const reference. This makes it possible to use move operators when you add to a temporary.
Implement in terms of operator +=:
Matrix2D01 operator+(Matrix2D01 a, const Matrix2D01 &b)
{
a += b;
return a;
}
I've posted the code of the Matrix class I had written for an assignment on Discrete Mathematics last Sem
Notice how the assignment operator and the copy constructor are implemented All you need to do is write an assignment operator (I'd recommend that you write a copy constructor as well)
#pragma once
#include <vector>
#include <istream>
#include <ostream>
class Matrix
{
private:
unsigned int rows, cols;
std::vector<std::vector<int> > matrix;
public:
// Creates a Matrix with the given dimensions and sets the default value of all the elements to 0
Matrix(unsigned int rows, unsigned int cols);
// Destructor
~Matrix();
// Converts the relation set into it's adjacency Matrix representation
static Matrix CreateFromRelationSet( std::vector<std::pair<int, int> > relationSet);
// Copy Constructor
Matrix(const Matrix& cSource);
// Assignment Operator
Matrix& operator=(const Matrix& cSource);
// Resets all the elements of the matrix to 0
void Reset();
// Resizes the matrix to the given size
void Resize(unsigned int rows, unsigned int cols);
// Returns the number of rows
unsigned int getRows();
// Returns the number of columns
unsigned int getCols();
// Returns the smallest element from the matrix
int getSmallestElement();
// Returns the greatest element from the matrix
int getGreatestElement();
// Returns true if element is found and sets the row and column
// Returns false if not found
bool getPosition(int element, int* i, int* j);
// Deletes a row from the Matrix
void DeleteRow(unsigned int row);
// Deletes a column from the Matrix
void DeleteColumn(unsigned int col);
// Returns the element at (i,j)
int& operator()(unsigned int i, unsigned j);
// Returns the element at (i,j). Use the () operators instead
int getElementAt(unsigned int i, unsigned j);
friend std::ostream& operator << (std::ostream& out, Matrix& m);
friend std::istream& operator >> (std::istream& in, Matrix& m);
Matrix operator + (Matrix M);
Matrix operator - (Matrix M);
Matrix operator * (Matrix M);
};
// For use with cin & cout
std::ostream& operator << (std::ostream& out, Matrix& m);
std::istream& operator >> (std::istream& in, Matrix& m);
#include "Matrix.h"
Matrix::Matrix(unsigned int rows, unsigned int cols)
{
this->rows = rows;
this->cols = cols;
matrix.resize(rows);
for(std::vector<int>& i: matrix)
i.resize(cols);
Reset();
}
Matrix::~Matrix()
{
}
// local helper function
int findMaxFromSet(std::vector<std::pair<int, int> > set)
{
int maxVal = 0;
for(unsigned int i = 0; i < set.size(); i ++)
{
int p = set[i].first > set[i].second ? set[i].first : set[i].second;
maxVal = maxVal > p ? maxVal : p;
}
return maxVal;
}
Matrix Matrix::CreateFromRelationSet (std::vector<std::pair<int, int> > relationSet)
{
int max = findMaxFromSet(relationSet);
Matrix M(max,max);
M.Reset();
for(auto i = relationSet.begin(); i != relationSet.end(); ++ i)
M(i->first - 1, i->second - 1) = 1;
return M;
}
void Matrix::Reset()
{
for (auto& i: matrix)
for(auto& j: i)
j = 0;
}
void Matrix::Resize(unsigned int rows, unsigned int cols)
{
matrix.resize(rows);
for(auto& i:matrix)
i.resize(cols);
}
unsigned int Matrix::getRows()
{
return rows;
}
unsigned int Matrix::getCols()
{
return cols;
}
Matrix::Matrix(const Matrix& cSource)
{
rows = cSource.rows;
cols = cSource.cols;
matrix = cSource.matrix;
}
bool Matrix::getPosition(int element, int* i, int* j)
{
for(unsigned int ii = 0; ii < getRows(); ii ++)
for(unsigned int jj = 0; jj < getCols(); jj ++)
if(matrix[ii][jj] == element)
{
*i = ii;
*j = jj;
return true;
}
return false;
}
Matrix& Matrix::operator=(const Matrix& cSource)
{
// check for self-assignment
if (this == &cSource)
return *this;
if(this->getRows() < cSource.rows)
{
this->rows = cSource.rows;
this->matrix.resize(cSource.rows);
}
if(this->getCols() < cSource.cols)
{
this->cols = cSource.cols;
for(auto& i:this->matrix)
i.resize(cSource.cols);
}
for(unsigned int i = 0; i < rows; i++)
for(unsigned int j = 0; j < cols; j++)
this->matrix[i][j] = const_cast<Matrix&>(cSource)(i,j);
return *this;
}
std::ostream& operator << (std::ostream& out, Matrix& m)
{
for(auto& i: m.matrix)
{
for(auto& j: i)
out<<j<<'\t';
out<<std::endl;
}
return out;
}
std::istream& operator >> (std::istream& in, Matrix& m)
{
for(auto& i: m.matrix)
for(auto& j: i)
in>>j;
return in;
}
Matrix Matrix::operator + (Matrix op)
{
// Find the rows and cols of the new matrix
unsigned int r = this->getRows() > op.getRows()?this->getRows():op.getRows();
unsigned int c = this->getCols() > op.getCols()?this->getCols():op.getCols();
// Create Matrices
Matrix A = *this;
Matrix B = op;
Matrix R(r,c);
// Assign values
for(unsigned int i = 0; i < A.rows; i++)
for(unsigned int j = 0; j < A.cols; j++)
R(i,j) = A(i,j) + B(i,j);
return R;
}
Matrix Matrix::operator - (Matrix op)
{
// Find the rows and cols of the new matrix
unsigned int r = this->getRows() > op.getRows()?this->getRows():op.getRows();
unsigned int c = this->getCols() > op.getCols()?this->getCols():op.getCols();
// Create Matrices
Matrix A = *this;
Matrix B = op;
Matrix R(r,c);
// Assign values
for(unsigned int i = 0; i < A.rows; i++)
for(unsigned int j = 0; j < A.cols; j++)
R(i,j) = A(i,j) - B(i,j);
return R;
}
Matrix Matrix::operator* (Matrix op)
{
Matrix A = *this;
Matrix B = op;
if(A.getCols() != B.getRows())
throw std::exception("Matrices cannot be multiplied");
Matrix M(A.getRows(), B.getCols());
for(unsigned int i=0 ; i<A.getRows() ; i++)
for(unsigned int j=0 ; j<B.getCols() ; j++)
for(unsigned int k=0 ; k<B.getRows() ; k++)
M(i,j) = M(i,j) + A(i,k) * B(k,j);
return M;
}
int& Matrix::operator()(unsigned int i, unsigned j)
{
return matrix[i][j];
}
int Matrix::getElementAt(unsigned int i, unsigned j)
{
return (*this)(i,j);
}
int Matrix::getSmallestElement()
{
int result = matrix[0][0];
for(auto i:matrix)
for(auto j : i)
if(j < result)
result = j;
return result;
}
int Matrix::getGreatestElement()
{
int result = matrix[0][0];
for(auto i:matrix)
for(auto j : i)
if(j > result)
result = j;
return result;
}
void Matrix::DeleteRow(unsigned int row)
{
matrix.erase(matrix.begin() + row);
rows --;
}
void Matrix::DeleteColumn(unsigned int col)
{
for(auto& i: matrix)
i.erase(i.begin() + col);
cols --;
}
UPDATE:
result=*this;
In your code you are trying to allocate result the value of *this using the assignment operator, but no assignment operator is defined and the compiler doesn't find a match
Writing an assignment operator overload will solve this issue.
And if you have a copy constructor you can do something like:
Matrix2D01 Matrix2D01::operator+(Matrix2D01& mat2) {
Matrix2D01 result = *this;
result+=mat2;
return result;
}
Writing Assignment Operators: http://www.learncpp.com/cpp-tutorial/912-shallow-vs-deep-copying/
And if you're interested in the details - here you go: http://www.icu-project.org/docs/papers/cpp_report/the_anatomy_of_the_assignment_operator.html
Your operands should be const references. Otherwise, you
can't use a temporary to initialize them. (In this case, the
return value of operator+ is a temporary, and thus, cannot be
bound to the non-const reference of operator=.)
If your operator+ is a member, it should be const as well.
After all, it doesn't modify the object it is called on. So:
Matrix2D01& Matrix2D01::operator=( Matrix2D01 const& mat2 );
Matrix2D01& Matrix2D01::operator+=( Matrix2D01 const& mat2 );
Matrix2D01 Matrix2D01::operator+( Matrix2D01 const& mat2 ) const;
Also, your assignment operator is broken. (Think of what will
happen, if one of the allocations fails after you've freed the
original data.) In general, if you have to test for self
assignment, the assignment operator is broken.

Class inheritance, copy constructor and set/get functions

I got the following class:
class Matrix{
private:
int rows;
int columns;
double* matrix;
public:
Matrix();
explicit Matrix(int N);
Matrix(int M, int N);
void setValue(int M, int N, double value);
double getValue(int M, int N);
bool isValid() const;
int getRows();
int getColumns();
~Matrix();
friend ostream& operator<<(ostream &out, Matrix&matrix1);
Matrix &operator=(const Matrix &m) {
if (rows * columns != m.rows * m.columns){
delete [] this->matrix;
this->matrix = new double[m.rows * m.columns];
}
rows = m.rows;
columns = m.columns;
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
this->matrix[i * columns + j] = m.matrix[i * columns + j];
}
}
return *this;
}
Matrix(const Matrix &rhs);
};
with these functions
#include <iostream>
#include "Matrix.h"
using namespace std;
//OPPGAVE 2
Matrix::Matrix(){
matrix = NULL;
}
Matrix::Matrix(int N){
matrix = new double[N * N];
rows = N;
columns = N;
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
if(i==j)
matrix[i * N + j] = 1;
else
matrix[i * N + j] = 0;
}
}
}
Matrix::Matrix(int M, int N){
matrix = new double[M * N];
rows = M;
columns = N;
for(int i = 0; i < M; i++){
for(int j = 0; j < N; j++)
matrix[i * N + j] = 0;
}
}
Matrix::~Matrix(){
delete [] matrix;
}
void Matrix::setValue(int M, int N, double value){
matrix[M * columns + N] = value;
}
double Matrix::getValue(int M, int N){
return matrix[M * columns + N];
}
bool Matrix::isValid() const{
if(matrix==NULL)
return false;
else
return true;
}
int Matrix::getRows(){
return rows;
}
int Matrix::getColumns(){
return columns;
}
ostream& operator<<(ostream &out, Matrix&matrix1){
if(matrix1.isValid())
for(int i = 0; i < matrix1.getRows(); i++){
for(int j = 0; j < matrix1.getColumns(); j++)
out << matrix1.getValue(i,j) << "\t";
out << endl;
}
else
out << "Matrisen er ikke gyldig." << endl;
return out;
}
Matrix::Matrix(const Matrix &rhs) : rows(rhs.rows),
columns(rhs.columns),
matrix(new double[rows * columns]) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
this->matrix[i * columns + j] = rhs.matrix[i * columns + j];
}
}
}
The exercise says:
a) Create a class Vector that inherits the MxN Matrix.
We want to use the Vector class as an interface to an Mx1 dimensional matrix with some
extra functionality.
b) Implement the following constructors for the Vector class.
• Vector()
Default constructor, should initialize the underlying matrix into the invalid state.
• explicit Vector(unsigned int N)
Should construct the underlying Mx1 Matrix, initialized as a zero-matrix. (The explicit keyword is not in the syllabus, but it should be used here.)
• Vector(const Matrix & other);
Copy-constructor from Matrix. Should assign a matrix to *this if and only if the matrix has dimensions Nx1, otherwise the resulting *this should be set to invalid. Hint: Reuse operator= from the Matrix-class.
This is what I've got so far:
#include "Matrix.h"
class Vector : public Matrix{
public:
Vector();
explicit Vector(int N);
Vector(const Matrix & other);
};
and
using namespace std;
#include <iostream>
#include "Vector.h"
Vector::Vector()
:Matrix(){ }
Vector::Vector(int N)
:Matrix(N,1){ }
How am I supposed to reuse the operator= from Matrix? If I try to copy it from the Matrix class into the Vector class, it says that rows, columns etc is inaccessible. How do I access these?
Is it possible to write a copy constructor for the Vector class more or less the same as the copy constructor for the Matrix class? They are both arrays, so I guess it should work?
Will the operators I overloaded for Matrix (not included here) automaticly be used if I multiply a Matrix with a Vector, or do I also need to include these somehow in the Vector class? (They were written outside the Matrix class in the Matrix.cpp-file.)
Next Im going to write set and get functions for the Vector class.
Is it possible to write these functions on this form?:
void Vector::setValue(int i, double value) {
Matrix::setValue(i, 1, value);
}
Help and tips are greatly appreciated!
What follows is hideous kludgery to satisfy an incompetent professor. Don't do this in the real world.
First, the misnamed "copy" constructor. If we weren't worried about the dimensions, we could do this (shudder):
Vector(const Matrix & other)
{
*this = other;
}
But we must check the dimensions first. We could do it this way:
Vector(const Matrix & other)
{
if(other.getColumns()==1)
*this = other;
}
But some chucklehead neglected to make getColumns() const, so this results in a compiler error. We could do something truly drastic, const cast:
Vector(const Matrix & other)
{
Matrix *p = const_cast<Matrix *>(&other);
if(p->getColumns()==1)
*this = other;
}
Or just something facepalmingly awful:
Vector(const Matrix & other)
{
Matrix M(other); // notice that this is not const
if(M.getColumns()==1)
*this = other;
}
Do you need help with the isValid stuff?
You are on the right track for the sets and gets. You can call operators with member function like syntax Class::operator*(args). Implementing the vector assignment would look something like this:
Vector & Vector::operator=(const Vector &v){
Matrix::operator=(v);
return *this;
}
You will want your Vector constructors to be declared public. I am thinking because you are using inheritance the compiler will generate correct copy constructors and assignment operators for the Vector class. You should write tests to verify this assumption.

Not working matrix operator+ overloading

I'm trying to make a example (just example! I know it leaks) app to learn operator overloading in C++, but I get the value of zero to the elements of the sum.... I suspect that the problem is int the copy constructor.
The Detailed implementation is below:
class Matrix{
public:
Matrix(int row, int col);
Matrix(const Matrix& src);
float& set(int row, int col);
float get(int row, int col);
const Matrix & operator+(const Matrix& rhs);
private:
float* data;
int nrow;
int ncol;
};
Matrix::Matrix(int row, int col){
nrow = row;
ncol = ncol;
data = new float[nrow*ncol];
}
Matrix::Matrix(const Matrix& src){
nrow = src.nrow;
ncol = src.ncol;
data = new float[nrow*ncol];
for(int i = 0; i < nrow*ncol; i++){
data[i] = src.data[i];
}
}
float& Matrix::set(int row, int col){
return data[row*ncol+col];
}
float Matrix::get(int row, int col){
return data[row*ncol+col];
}
const Matrix & Matrix::operator+(const Matrix& rhs){
if (this->nrow == rhs.nrow && this->ncol == rhs.ncol){
Matrix* m = new Matrix(rhs.nrow, rhs.ncol);
for(int i=0; i< nrow*ncol; i++){
m->data[i] = data[i] + rhs.data[i];
}
return *m;
} else {
throw -1;
}
}
#include <iostream>
using namespace std;
int main ()
{
Matrix A(1,1);
Matrix B(1,1);
A.set(0,0)=1;
B.set(0,0)=2;
cout << A.get(0,0) << endl;
cout << B.get(0,0) << endl;
Matrix C = A + B; // Marix C(A+B);
cout << C.get(0,0) << endl;
return 0;
}
Matrix::Matrix(int row, int col){
nrow = row;
ncol = ncol;
data = new float[nrow*ncol];
}
There's a typo in there that results in your code having undefined behavior.
Fix it with:
ncol = col;
(Make sure you turn you compilers warning/diagnostics level to the maximum, GCC catches this.)
You're leaking your float[]s too, so your code isn't complete. Don't forget to add proper destructors, and always follow the rule of three.