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();
Matrix(const Matrix& m); // Copy-Konstruktór
int setze_element(int zeile, int spalte, double Wert_setzen);
Matrix transpos();
friend ostream& operator << (ostream& stream, Matrix& m);
Matrix operator = (const Matrix& m); // Zuweisungsoperator assignment operator
};
Matrix Matrix::operator= (const Matrix& M) // assigment
{
werte = M.werte;
return *this;
}
Matrix::Matrix(const Matrix& M) //Copy
{
Zeile = M.Zeile;
Spalte = M.Spalte;
werte = M.werte;
}
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.