In the code example I have described with comments my problem. I want to transpose a matrix from the method transpos(). This works but I have no idea how to pass the transposed matrix to the original object. The transposed matrix exists only in the method.
#include <iostream>
#include <string.h>
using namespace std;
class Matrix
{
private:
int Zeile, Spalte;
double** werte;
public:
Matrix();
Matrix(int zeilen, int spalten);
~Matrix();
int setze_element(int zeile, int spalte, double Wert_setzen);
Matrix transpos();
friend ostream& operator << (ostream& stream, Matrix& m);
};
Matrix::Matrix(int zeilen, int spalten)
{
this->Spalte = spalten;
this->Zeile = zeilen;
werte = new double* [Spalte];
for (int r = 0; r < Zeile; r++)
{
werte[r - 1] = new double[Spalte];
}
double init_mit = 0;
for (int r = 0; r < Zeile; r++)
{
for (int c = 0; c < Spalte; c++)
{
//cout << "Enter ..."; //cin >> d;
werte[r - 1][c - 1] = init_mit;
cout << werte[r - 1][c - 1] << "\t";
}
cout << endl;
}
}
Matrix::Matrix()
{
Zeile = Spalte = 0;
werte = NULL; //Null-pointer
}
Matrix::~Matrix()
{
}
int Matrix::setze_element(int zeile, int spalte, double Wert_setzen)
{
this->Zeile;
this->Spalte;
if (zeile < this->Zeile && spalte < this->Spalte)
{
werte[zeile - 1][spalte - 1] = Wert_setzen;
return 1;
}
else
{
cout << "Wert Ausserhalb der Matrix. Abbruch!" << endl;
return 0;
}
}
Matrix Matrix::transpos() // r = row = zeile / c = col = Spalte
{
int MT_zeile, MT_spalte = 0;
MT_zeile = Spalte;
MT_spalte = Zeile;
cout << "Shows transpose Matrix by default." << endl;
Matrix MT(MT_zeile, MT_spalte); // init the new Matrix(3/5) original matrix is (5/3)
for (int r = 0; r < Zeile; r++)
{
for (int c = 0; c < Spalte; c++)
{
MT.werte[c - 1][r - 1] = werte[r - 1][c - 1]; // works well
}
}
cout << endl;
// i dont know how i give the transpose matrix back, the matrix is only here in the methode
return MT;
}
ostream& operator << (ostream& stream, Matrix& m)
{
for (int r = 0; r < m.Zeile; r++) {
for (int c = 0; c < m.Spalte; c++)
{
stream << m.werte[r - 1][c - 1] << "\t";
}
stream << endl;
}
return stream;
}
int main()
{
Matrix M(5, 3); // will show on consle the init matrix by default
cout << endl;
cout << "Transpose starts here." << endl; //transpose starts here
M.setze_element(0, 2, 5); // i set a element in row 1 and col 3 to the value of 5 to see if the transpose works
cout << "Element is set to value 5." << endl;
cout << M << endl;
M.transpos(); // the given method for transpose
cout << "Now should show the transpose matrix with the object values." << endl;
cout << M; // print the original matrix not the transpos, not what i need
cout << endl;
return 0;
}
I try to return the pointer of the object or the object itself.
>Solution :
You already do:
// i dont know how i give the transpose matrix back, the matrix is only here in the methode
return MT;
What you need to change is the usage:
M = M.transpos();
.tranpos() returns the transposed matrix. The M = part assigns it. Be wary, though, if you didn’t follow the rule of 3, this will expose existing issues in your code.
Alternatively, you could make it so that transpose modifies the object it is called on, rather than returning the transposed matrix.
*this = MT;
return MT;
But, again, this will expose issues due to you not having a proper assignment operator (rule of three).