rvalue and reference please help MEEE

#include <iostream>
/*m and n is the matrix size and T is the type of the matrix blocks*/
/*the default type of matrix is int */
template<int m, int n, typename T = int>
class Matrix {
private:
T * matrix;/*matrix is a pointer if T type*/
int _m, _n;
public:
///////////////////////////* distructor *////////////////////////////
~Matrix<m,n,T>(){
delete[] matrix;
}
/////*constructor if it get nothing as K it will fill with zeros*////
Matrix<m, n, T>(T k = 0) : _m(m), _n(n) {
matrix = new T[_m*_n];
int i = 0;
for (; i<_m*_n; i++)
matrix[i] = k;
}
/////////////////////*copy constructor *////////////////////////////
Matrix<m, n, T> (Matrix<m, n, T>& other): _m(other._m), _n(other._n)
{
delete[] matrix;
this->matrix = new T[_m*_n];
for (int i = 0; i < _m*_n; i++)
this->matrix[i] = other.getmatrix(i);
}
/////////*function to calculate the trace of the matrix*//////////////
T trace() {
T sum = 0;
int number = (_m>_n) ? _n : _m;
for (int i = 0; i<number; i++) {
sum += matrix[i*(_n)+i];
}
return sum;
}
///////*geters will return the T vale in the i place in matrix*////////
const T getmatrix (const int i) { return matrix[i]; }
T* getDiag(int&number) {
int i = 0;
number = (_m>_n) ? _n : _m;
T* diang = new T[number];
for (; i<number; i++) {
diang[i] = matrix[i*(_n)+i];
}
return diang;
}
///////////////////////*overload Ostream operator*//////////////////////////
friend std::ostream & operator<<(std::ostream & cout,const Matrix<m, n, T>&matrix) {
int i = 0;
for (; i<matrix._m*matrix._n; i++) {

std::cout << matrix.matrix[i];
// if((i+1)%matrix._n)
std::cout << " ";
if (i && !((i + 1) % matrix._n) && i != matrix._m*matrix._n)
std::cout << std::endl;
}
return cout;
}
/////////////*friend overload + operator for T + matrix*//////////////
friend Matrix<m, n, T> operator +(T num, Matrix<m, n, T>&other) {
Matrix result(other);
int i = 0;
for (; i<result._m*result._n; i++)
result.matrix[i] += num;
return result;
}
///////////////*overload + operator matrix + matrix*/////////////////
Matrix<m, n, T> operator+(Matrix<m, n, T>&other) {
Matrix result(*this);
int i = 0;
for (; i<_m*_n; i++)
result.matrix[i] += other.matrix[i];
return result;
}
/////////////////*overload + operator matrix * T/////////////////////
Matrix<m, n, T> operator *(T num) {
Matrix result(*this);
int i = 0;
for (; i<_m*_n; i++)
result.matrix[i] *= num;
return result;
}
////////////////////////*overload = operator*/////////////////////////
/*Matrix<m, n, T>& operator =(Matrix<m, n, T> other) {
for (int i = 0; i<_m*_n; i++)
matrix[i] = other.getmatrix(i);
return this;
}*/
Matrix<m, n, T>& operator =(Matrix<m, n, T> other) {
this->~Matrix<m, n, T>();
this(other);
return this;
}
//////////////////////*overload matrix ++ operator*/////////////////////
Matrix<m, n, T> operator++(int) {
Matrix temp(*this);
++(*this);
return temp;
}
//////////////////////*overload matrix -- operator*/////////////////////
Matrix<m, n, T> operator--(int) {
Matrix temp (*this);
--(*this);
return temp;
}
///////////////////////*overload () operator*/////////////////////////////////
T& operator()(int col, int row) {
return matrix[(col)*(_n)+row];
}
//////////////////////*overload matrix++ operator*////////////////////////////
Matrix<m, n, T>& operator++() {
for(int i=0;i<this->_m*this->_n;i++){
this->matrix[i]++;
}
return *this;
}
//////////////////////*overload matrix-- operator*////////////////////////////
Matrix<m, n, T>& operator--() {
for(int i=0;i<this->_m*this->_n;i++){
this->matrix[i]--;
}
return *this;
}
//////////////////////*overload - operator matrix-T*////////////////////////////
Matrix<m, n, T> operator -(T num) {
Matrix temp(*this);
for(int i=0;i<temp._m*temp._n;i++){
temp.matrix[i]=-num;
}
return temp;
}
//////////////////////*overload -1*matrix operator*//////////////////////////////
Matrix<m, n, T> operator-() {
Matrix temp(*this);
for(int i=0;i<temp._m*temp._n;i++){
temp.matrix[i]*=-1;
}
return temp;
}
};
template <int row, int col, typename T>
void printDiag(Matrix<row, col, T>& mat) {
int number;
T* diag = mat.getDiag(number);
for (int i = 0; i<number; i++)
{
std::cout << diag[i] << " ";
}
std::cout << std::endl;
delete[] diag;
}

int main() {

//freopen("output_matrix.txt", "w", stdout);

Matrix<4, 4> mat;
std::cout << mat << std::endl;

Matrix<4, 4> identity(1);
std::cout << identity << std::endl;

Matrix<4, 4> res = (identity + 2) * 3;
std::cout << res << std::endl;

Matrix<3, 4> mat1(9);
Matrix<3, 4> mat2(4);

std::cout << (mat1 - 4) << std::endl;

mat2(2, 1) = 0;

const int cell = (2 + mat1 - 1)(1, 2);

std::cout << "The value of cell 1, 2 is: " << cell << std::endl;

std::cout << (1 + mat2) << std::endl;

std::cout << ++mat1 << std::endl;
std::cout << --mat2 << std::endl;

std::cout << "Values of mat1 and mat2, before..." << std::endl;
std::cout << mat1++ << std::endl;
std::cout << mat2++ << std::endl;

std::cout << "Values of mat1 and mat2, after..." << std::endl;
std::cout << mat1 << std::endl;
std::cout << mat2 << std::endl;

mat2(0,0) = 13;

/*
* Matrix diagonal is:
* 1. In case of nxn matrix it's simple all (i, i) cell for all
* i between 0 and n - 1.
* 2. In case of nxm matrix it's all cells (i, i) cells for all
* i between 0 and min(n,m).
*/
std::cout << "Printing main diagonal of mat2" << std::endl;
printDiag(mat2);

/*
*
* Trace of any given Matrix nxm is the sum of main
* diagonal entries.
*
* */
std::cout << "trace(mat2) = " << mat2.trace() << std::endl;
std::cout << std::endl;

Matrix<3, 2, double> m1, m2;

m1(0,0)=1; m1(0,1) = -1; // 1 -1
m1(1,0)=3; m1(1,1) = 7; // 3 7
m1(2,0)=-5; m1(2,1) = 0; // -5 0

std::cout << "Matrix m1: " << std::endl;
std::cout << m1 << std::endl;
std::cout << "Printing main diagonal of m1" << std::endl;
printDiag(m1);
std::cout << "trace(m1) = " << m1.trace() << std::endl;
std::cout << std::endl;

m2(0,0)=3; m2(0,1) = 1; // 3 1
m2(1,0)=-1; m2(1,1) = 2; // -1 2
m2(2,0)=0; m2(2,1) = 6; // 0 6

std::cout << "Matrix m2: " << std::endl;
std::cout << m2 << std::endl;
std::cout << "Printing main diagonal of m2" << std::endl;
printDiag(m2);
std::cout << "trace(m2) = " << m2.trace() << std::endl;
std::cout << std::endl;

//Matrix<3, 2, double> m3 = m1 + m2;
Matrix<3, 2, double> m3;
m3=m1+m2;
std::cout << "Matrix m3 = m1 + m2: " << std::endl;
std::cout << m3 << std::endl;
std::cout << "Printing main diagonal of m3" << std::endl;
printDiag(m3);
std::cout << "trace(m3) = " << m3.trace() << std::endl;
std::cout << std::endl;

Matrix<3, 2, double> m4 = m3 + 1 - m2*2 + m1;
++m4;
++m4;

std::cout << "Matrix m4 = m3 + 1 - 2*m2 + m1" << std::endl;
std::cout << "++m4" << std::endl;
std::cout << "m4:" << std::endl;
std::cout << m4 << std::endl;
std::cout << "Printing main diagonal of m4" << std::endl;
printDiag(m4);
std::cout << "trace(m4) = " << m4.trace() << std::endl;
std::cout << std::endl;


Matrix<2, 4, float> mf1(2.5f);

mf1(0,0) += 3; mf1(0, 3) -= 0.5f;
mf1(1,1) = 0; mf1(1, 2) = 0;
++mf1;
mf1 = (-mf1 + 1.7f) + mf1*0.25f - 1;
std::cout << "Matrix mf1:" << std::endl;
std::cout << mf1 << std::endl;
std::cout << "Printing main diagonal of mf1" << std::endl;
printDiag(mf1);
std::cout << "trace(mf1) = " << mf1.trace() << std::endl;
std::cout << std::endl;

return 0;
}

the operator = got some problems no matter the how i overload it
the main was giving to me i had to make the classes and the function
and i can only make return for values without using new function otherwise i will get memory leak

so the problem i get is with this kind of expression
Matrix<4, 4> res = (identity + 2) * 3;
and the problem i get is
invalid initialization of non-const reference of type ‘Matrix<4, 4>&’ from an rvalue of type ‘Matrix<4, 4>’
i know that i got and temporary value and like i cant send it to operator =
please help me fix it
Hello moo2401,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I am nor sure how to answer this problem yet. I did find the overloaded "+" operator is expecting (T num, Matrix<m, n, T>&other), but you are sending ( Matrix<m, n, T>&other, T num). It is backwards.

For the line you mentioned Matrix<4, 4> res = (identity + 2) * 3; I believe the problem went away when I changed the overloaded "+" operator.

The only error I have left is with Matrix<3, 2, double> m4 = (m3 + 1) - (m2 * 2) + m1; // <--- Added (). The "-" operator here is the problem. There is no overload for what you are doing here. I believe in the end it is a "Matrix - Matricx".

Hope that helps,

Andy
Topic archived. No new replies allowed.