如何从方法返回转置矩阵给对象?

huangapple go评论81阅读模式
英文:

How can I return the transposed matrix from the method to the object?

问题

以下是您提供的代码的翻译部分:

#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);                  // 复制构造函数
    int setze_element(int zeile, int spalte, double Wert_setzen);
    Matrix transpos();
    friend ostream& operator << (ostream& stream, Matrix& m);
    Matrix operator = (const Matrix& m);    // 赋值运算符
};

Matrix Matrix::operator= (const Matrix& M) // 赋值
{
    werte = M.werte;
    return *this;
}

Matrix::Matrix(const Matrix& M)    // 复制
{
    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;       // 空指针
}

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 = 行 = Zeile / c = 列 = Spalte
{
    int MT_zeile, MT_spalte = 0;
    MT_zeile = Spalte;
    MT_spalte = Zeile;
    cout << "默认情况下显示转置矩阵。" << endl;
    Matrix MT(MT_zeile, MT_spalte);         // 初始化新矩阵(3/5) 原始矩阵是(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];       // 运行良好
        }
    }
    cout << endl;
    // 我不知道如何将转置矩阵返回,矩阵仅存在于这个方法中
    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); // 默认情况下会在控制台上显示初始化矩阵
    cout << endl;
    cout << "开始转置。" << endl;       // 开始转置
    M.setze_element(0, 2, 5);       // 将第1行第3列的元素设置为5,以查看转置是否有效
    cout << "元素设置为值5。" << endl;
    cout << M << endl;
    M.transpos();               // 给定的转置方法
    cout << "现在应该显示对象值的转置矩阵。" << endl;
    cout << M;                      // 打印原始矩阵而不是转置矩阵,不是我需要的
    cout << endl;
    return 0;
}

希望这有助于您理解代码的翻译部分。如果您需要进一步的帮助,请随时提出问题。

英文:

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 &lt;iostream&gt;
#include &lt;string.h&gt;
using namespace std;
class Matrix
{
private:
int Zeile, Spalte;
double** werte;
public:
Matrix();
Matrix(int zeilen, int spalten);
~Matrix();
Matrix(const Matrix&amp; m);				  // Copy-Konstrukt&#243;r
int setze_element(int zeile, int spalte, double Wert_setzen);
Matrix transpos();
friend ostream&amp; operator &lt;&lt; (ostream&amp; stream, Matrix&amp; m);
Matrix operator = (const Matrix&amp; m);    // Zuweisungsoperator assignment operator
};
Matrix Matrix::operator= (const Matrix&amp; M) // assigment
{
werte = M.werte;
return *this;
}
Matrix::Matrix(const Matrix&amp; M)	//Copy
{
Zeile = M.Zeile;
Spalte = M.Spalte;
werte = M.werte;
}
Matrix::Matrix(int zeilen, int spalten)
{
this-&gt;Spalte = spalten;
this-&gt;Zeile = zeilen;
werte = new double* [Spalte];
for (int r = 0; r &lt; Zeile; r++)
{
werte[r - 1] = new double[Spalte];
}
double init_mit = 0;
for (int r = 0; r &lt; Zeile; r++)
{
for (int c = 0; c &lt; Spalte; c++)
{
//cout &lt;&lt; &quot;Enter ...&quot;; //cin &gt;&gt; d;
werte[r - 1][c - 1] = init_mit;
cout &lt;&lt; werte[r - 1][c - 1] &lt;&lt; &quot;\t&quot;;
}
cout &lt;&lt; endl;
}
}
Matrix::Matrix()
{
Zeile = Spalte = 0;
werte = NULL;       //Null-pointer
}
Matrix::~Matrix()
{
}
int Matrix::setze_element(int zeile, int spalte, double Wert_setzen)
{
this-&gt;Zeile;
this-&gt;Spalte;
if (zeile &lt; this-&gt;Zeile &amp;&amp; spalte &lt; this-&gt;Spalte)
{
werte[zeile - 1][spalte - 1] = Wert_setzen;
return 1;
}
else
{
cout &lt;&lt; &quot;Wert Ausserhalb der Matrix. Abbruch!&quot; &lt;&lt; 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 &lt;&lt; &quot;Shows transpose Matrix by default.&quot; &lt;&lt; endl;
Matrix MT(MT_zeile, MT_spalte);         // init the new Matrix(3/5)  original matrix is (5/3)
for (int r = 0; r &lt; Zeile; r++)
{
for (int c = 0; c &lt; Spalte; c++)
{
MT.werte[c - 1][r - 1] = werte[r - 1][c - 1];       // works well 
}
}
cout &lt;&lt; endl;
// i dont know how i give the transpose matrix back, the matrix is only here in the methode 
return MT;
}
ostream&amp; operator &lt;&lt; (ostream&amp; stream, Matrix&amp; m)
{
for (int r = 0; r &lt; m.Zeile; r++) {
for (int c = 0; c &lt; m.Spalte; c++)
{
stream &lt;&lt; m.werte[r - 1][c - 1] &lt;&lt; &quot;\t&quot;;
}
stream &lt;&lt; endl;
}
return stream;
}
int main()
{
Matrix M(5, 3); // will show on consle the init matrix by default 
cout &lt;&lt; endl;
cout &lt;&lt; &quot;Transpose starts here.&quot; &lt;&lt; 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 &lt;&lt; &quot;Element is set to value 5.&quot; &lt;&lt; endl;
cout &lt;&lt; M &lt;&lt; endl;
M.transpos();               // the given method for transpose 
cout &lt;&lt; &quot;Now should show the transpose matrix with the object values.&quot; &lt;&lt; endl;
cout &lt;&lt; M;                      // print the original matrix not the transpos, not what i need 
cout &lt;&lt; endl;
return 0;
}

I try to return the pointer of the object or the object itself.

答案1

得分: 0

你已经完成了:
// 我不知道如何返回转置矩阵,矩阵只在方法中
return MT;
你需要修改的是使用方式:
M = M.transpos();
`.tranpos()` 返回转置矩阵。`M =` 部分将其赋值。但请注意,如果你没有遵循三大法则,这会暴露出代码中现有的问题。
或者,你可以让转置 _修改_ 调用它的对象,而不是返回转置矩阵。
*this = MT;
return MT;
但是,再次强调,如果你没有一个合适的赋值运算符(三大法则),这会暴露出问题。
英文:

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).

huangapple
  • 本文由 发表于 2023年5月20日 21:20:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295450.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定