vendredi 6 mars 2015

Overloading operator= doesn't compile with assigning

github repo with code Trying to code Matrix class with overloading of some operations.

When I trying to compile with this stroke everything is going wrong



result = (l_mtx + r_mtx);


I get error from g++:

g++ -g3 -std=c++11 -Wall -o matrix matrix_class.h matrix.cpp


matrix.cpp: In function ‘int main()’:

matrix.cpp:36:12: error: no matching function for call to ‘Matrix::Matrix(Matrix)’



result = (l_mtx + r_mtx);


and then goes several candidates for this function, which I don't really understand.

There are I think copy constructor and several constructors, but this is not operator= which I think supposed to cal in that stroke.


matrix_class.h:73:5: note: Matrix::Matrix(Matrix&) [with type = double]

(no known conversion for argument 1 from ‘Matrix’ to ‘Matrix&’ )


matrix_class.h:46:5: note: Matrix::Matrix(int, int) [with type = double]

(candidate expects 2 arguments, 1 provided)


matrix_class.h:39:5: note: Matrix::Matrix() [with type = double]

(candidate expects 0 arguments, 1 provided)


and then the error:

matrix_class.h:96:18: error: initializing argument 1 of ‘Matrix Matrix::operator=(Matrix) [with type = double]’


I think I'm not correctly code assign operator or copy constructor, but I can't find where the error is. Sorry for dumb question. Thanks for paying attention.



//copy constructor
Matrix(const Matrix<type> &org)
{
cout << "Making a copy of " << this << endl;
row = org.getRow();
column = org.getColumn();

//allocate additional space for a copy
data = new type* [row];
for (int i = 0; i < row; ++i)
{
data[i] = new type [column];
}

for (int i = 0; i < row; ++i)
{
for (int j = 0; j < column; ++j)
{
data[i][j] = org.data[i][j];
}
}
}


and operator=



//assign constructor
Matrix<type> operator = (Matrix<type> r_mtx)
{
if (row == r_mtx.getRow())
{
if (column == r_mtx.getColumn())
{
//TODO: удалить прежний объект?
Matrix<type> temp(row, column);
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < column; ++j)
{
temp.data[i][j] = r_mtx[i][j];
}
}
return temp;
}
else
{
cout << "Assign error: matrix column are not equal!" << endl;
exit(EXIT_FAILURE);
}
}
else
{
cout << "Assign error: matrix rows are not equal!" << endl;
exit(EXIT_FAILURE);
}
}

Aucun commentaire:

Enregistrer un commentaire