英文:
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 <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.
答案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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论