【问题标题】:How to store values ​in 2-D array?如何将值存储在二维数组中?
【发布时间】: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函数将值保存在namenRownCol中。然后我们调用CMatrix x1 (name, nRow, nCol)来初始化x1namenRownCol。然后,我尝试通过x1.setElement() 函数放置一个元素,但我不断收到错误消息。如果你让我知道我错在哪里,我将不胜感激。

【问题讨论】:

  • 你说你“不断收到错误”... 什么“错误”?是构建错误吗?运行时错误或崩溃?出乎意料的输出?如果是构建错误,则将完整和完整的错误输出复制粘贴(作为文本!)到问题中,在出现错误的行上添加 cmets。如果它是一个崩溃,那么在调试器中捕获它并在你的代码中找到它发生的位置。用注释标记该行,并添加所有相关变量的值。同样对于运行时问题,包括您给程序的确切输入,以及可能的实际和预期输出。
  • 另外请花一些时间阅读the help pages,接受SO tour,阅读How to Ask,以及this question checklist
  • 顺便说一句,你包括&lt;vector&gt;但不使用std::vector?为什么不?除非这是您自己进行显式内存处理的作业或练习,否则您确实应该改用std::vector
  • 之所以使用数组,是因为问题条件如此。在 setElement() 函数部分,出现异常未处理的错误。你能知道代码中的问题吗?感谢您的回复——
  • edit您的问题包含该信息。加上你在运行时给程序的输入。 阅读我之前发布的链接。

标签: c++ arrays dynamic


【解决方案1】:

如果您还添加错误的输出,这可能会有所帮助。除此之外,我看不出为什么你会使用原始指针,或者不使用std::vector&lt;&gt;,或者只是简单地使用multi-dimensional arrays。另外,你有内存泄漏,请确保删除类析构函数中矩阵的所有new初始化值。

编辑:我看到您对问题和评论的编辑。对代码的编辑仍然有一个错误,但不是在setElement(),而是在void printMatrixinfo(),具体是这一行:

cout &lt;&lt; matrix[nRow][nCol] &lt;&lt; " ";

nRow 和 nCol 超出范围。从你的代码结构来看,我猜你想在这里改用cout &lt;&lt; matrix[i][j] &lt;&lt; " ";

同样,如果这是使用原始指针的问题条件,不要忘记在析构函数处删除矩阵(您的~CMatrix() 不应为空!)

【讨论】:

  • 之所以使用数组,是因为问题条件如此。在 setElement() 函数部分,出现异常未处理的错误。你能知道代码中的问题吗?感谢回复
  • 如果您没有发布 cmets 的声誉,请耐心等待。发布真正的评论作为答案只会让它被否决,让你失去声誉。
  • 我会小心的。谢谢你告诉我
  • 实际上,尝试运行@bongbong的更新代码。发现错误,所以我更新了我的答案。此外,bongbong 确保您的问题尽可能详细,您可能会得到更快的答案!祝你编码好运。是的,你是对的一些程序员老兄,应该耐心等待。我更新了我的答案,至少指出了需要修复的代码部分。如果这还不够好,我会删除我的答案。
  • 非常感谢。从现在开始,我会问你正确的问题。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-16
  • 1970-01-01
  • 1970-01-01
  • 2016-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多