【发布时间】:2020-05-22 10:24:14
【问题描述】:
#include<string>
#include<vector>
#include<iostream>
using namespace std;
class CMatrix
{
private:
string name;
float** matrix;
int nRow;
int nCol;
int nElement; //총 element 수
public:
CMatrix() {
this->name = "Anonymous";
this->nRow = 4;
this->nCol = 4;
matrix = new float* [nRow];
for (int i = 0; i < nRow; i++)
matrix[i] = new float[nCol];
for (int i = 0; i < nRow; i++)
for (int j = 0; j < nCol; j++)
matrix[i][j] = 0;
}
CMatrix(string _name, int _nRow, int _nCol) {
this->nRow = _nRow;
this->nCol = _nCol;
this->name = _name;
matrix = new float* [nRow];
for (int i = 0; i < nRow; i++)
matrix[nRow] = new float[nCol];
}
CMatrix(CMatrix& n1);
~CMatrix() {};
void setElement() {
cout << "<Enter the elements of A>" << endl << ">>";
for (int i = 0; i < nRow; i++) {
for (int j = 0; j < nCol; j++)
cin >> matrix[i][j];
}
}
void printMatrixinfo() {
cout << this->name << '(' << this->nRow << ", " << this->nCol << ", " << nRow * nCol << ") " <<
endl;
for (int i = 0; i < nRow; i++) {
cout << "[ ";
for (int j = 0; j < nCol; j++)
cout << matrix[nRow][nCol] << " ";
cout << "] ";
cout << endl;
}
}
string getName() {
return this->name;
}
};
void getData(string& _name, int& _nRow, int& _nCol) {
cout << "<Enter the name , # of rows, # of cols" << endl << ">>";
cin >> _name >> _nRow >> _nCol;
}
int main() {
string name;
int nRow, nCol;
getData(name, nRow, nCol);
CMatrix x1(name, nRow, nCol);
x1.setElement();
x1.printMatrixinfo();
cout << endl;
return 0;
}
首先,我们通过getData函数将值保存在name、nRow、nCol中。然后我们调用CMatrix x1 (name, nRow, nCol)来初始化x1的name、nRow、nCol。然后,我尝试通过x1.setElement() 函数放置一个元素,但我不断收到错误消息。如果你让我知道我错在哪里,我将不胜感激。
【问题讨论】:
-
你说你“不断收到错误”... 什么“错误”?是构建错误吗?运行时错误或崩溃?出乎意料的输出?如果是构建错误,则将完整和完整的错误输出复制粘贴(作为文本!)到问题中,在出现错误的行上添加 cmets。如果它是一个崩溃,那么在调试器中捕获它并在你的代码中找到它发生的位置。用注释标记该行,并添加所有相关变量的值。同样对于运行时问题,包括您给程序的确切输入,以及可能的实际和预期输出。
-
另外请花一些时间阅读the help pages,接受SO tour,阅读How to Ask,以及this question checklist。
-
顺便说一句,你包括
<vector>但不使用std::vector?为什么不?除非这是您自己进行显式内存处理的作业或练习,否则您确实应该改用std::vector。 -
之所以使用数组,是因为问题条件如此。在 setElement() 函数部分,出现异常未处理的错误。你能知道代码中的问题吗?感谢您的回复——
-
请edit您的问题包含该信息。加上你在运行时给程序的输入。 请阅读我之前发布的链接。