【问题标题】:Getting and Setting 2D Array Object Values (C++)获取和设置二维数组对象值 (C++)
【发布时间】:2016-07-09 04:00:52
【问题描述】:

我是 C++ 新手,二维数组的工作方式让我很困惑。我一直在网上阅读并试图了解导致我的具体问题的原因,但一无所获。


According to this Stack Overflow answer,我应该能够通过这样做在我的二维数组中获取一个值:(*myArrayObject)[row][col],但它会引发以下错误:

error: invalid types 'int[unsigned int]' for array subscript  
  return (*myArrayObject)[row][col];  
                                  ^  

如果我尝试执行myArrayObject[row][col],则会引发以下错误:

error: invalid initialization of non-const reference of types 'double&' from an rvalue of type 'double'  
  return myArrayObject[row][col];
                               ^  

这是有问题的完整(相关/简洁)代码:

main.cpp

#include "matrix.h"

using namespace std;

typedef unsigned int uint;

int main() {

 Matrix * matrix; //This could be the problem, but not sure what else to do
 matrix = new Matrix(10, 1);

 for(uint i = 0; i < matrix->numRows(); ++i) {
   for(uint j = 0; j < matrix->numCols(); ++j) {
     cout << matrix->at(i,j) << " " << endl;
   }
 }  
 return 0;
}

matrix.h

typedef unsigned int uint;  

class Matrix {  
public:
    Matrix(uint rows, uint cols); //Constructor
    const uint numRows() const;                  
    const uint numCols() const;                  

    void setRows(const uint &);                  
    void setCols(const uint &);                 

    double & at(uint row, uint col);
private:
    uint rows, cols;
    int ** matrix; //This could also be the problem, but not sure what else to do

    void makeArray() {
      matrix = new int * [rows];
      for(uint i = 0; i < rows; ++i) {
        matrix[i] = new int [cols];
      }
    }  
};

matrix.cpp

#include "matrix.h"

typedef unsigned int uint;

Matrix::Matrix(uint rows, uint cols) {
  //Make matrix of desired size
  this->setRows(rows);
  this->setCols(cols);

  //Initialize all elements to 0
  for(uint i = 0; i < rows; ++i) {
    for(uint j = 0; j < cols; ++j) {
      this->matrix[i][j] = 0;
    }
  }
}

const uint Matrix::numRows() const {
  return this->rows;
}

const uint Matrix::numCols() const {
  return this->cols;
}

void Matrix::setRows(const uint & rows) {
  this->rows = rows;
}

void Matrix::setCols(const uint & cols) {
  this->cols = cols;
}

double & Matrix::at(uint row, uint col) {
  return matrix[row][col]; //NOT WORKING
}

解决方案:
对 matrix.h 所做的更改:

double ** matrix;

void makeArray() {
  matrix = new double * [rows];
  for(uint i = 0; i < rows; ++i) {
    matrix[i] = new double [cols];
  }
}  

对 matrix.cpp 的更改:
在构造函数中添加了makeArray()

【问题讨论】:

  • myArrayObject[row][col] 似乎是晚餐的食物,但坦率地说,我会使用单维 std::vector&lt;double&gt; 并在 at 成员中进行行/列数学运算以获得正确的元素。顺便说一句,您将很难从声明为 int 的矩阵中返回 double &amp;

标签: c++ arrays object matrix multidimensional-array


【解决方案1】:
  1. 正如 WhozCraig 和 Rakete1111 所说,您的问题是,当您的数据全部为 int 时,您无法返回 referencedouble

但即使修复了它,您也可能会遇到其他问题。

  1. 您永远不会调用您的 makeArray 函数和/或在您的构造函数中您永远不会分配 (new) 数组,就像您在 makeArray 中所做的那样。

还不是问题,但是。

  1. 您不需要任何用于行和列值的设置器。或者,您需要以某种方式更改这些设置器,重新分配一个新矩阵以适应新的大小。

  2. #include 与复制和粘贴命名文件的内容相同。而且你有一个 uint 的 typedef,它只是从 matrix.h 复制粘贴到 matrix.cpp 和 main.cpp,所以即使你不再次指定它也可以工作。

  3. 你有一个using namespace std,但没有包含标准头。你可能需要那个东西,例如如果您 #include &lt;iostream&gt;&lt;vector&gt; 或任何其他标准库头文件。或者,如果出于某种原因,您在自己的 namespace std {...} 块中编写了代码。

【讨论】:

  • 我目前在 main.cpp 中获得的代码仅用于测试目的 - 我只是想看看该数组是否真的存储了任何东西,它是!另外,关于您的第三点,考虑到我的程序不会要求任何用户输入,这是否会成为一个问题?这一切都将直接输入 main.cpp 并执行。因此,如果需要创建一个新数组,它们将初始化并填充一个新数组。
  • @Mazzone 如果是这样,您仍然不需要二传手,因为您不会以这种方式使用它们 :)
【解决方案2】:

matrix[row][col] 是正确的做法,因为matrixint**,并且应用operator[] 两次就像将指针推迟两次,你会得到一个单独的int

因为at 返回double&amp; 而导致错误的原因。让我解释一下,matrix[row][col] 返回一个int,而这个int提升doubledouble 是一个临时变量,你不能从临时变量中引用,这就是编译器抱怨的原因。

at 返回int&amp; 显然可以解决它:)

【讨论】:

  • 在我的头文件中将int 的出现更改为double 后,它可以编译,但它仍然无法正常工作。现在它只是给我一个分段错误(核心转储)错误。
  • @Mazzone 你没有打电话给makeArray :)
  • 啊...当我之前尝试另一种方法时,我将它从我的 matrix.cpp 文件中取出。谢谢!一个更熟悉的头脑总是有帮助的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-13
  • 1970-01-01
  • 2016-09-29
  • 2018-12-30
  • 1970-01-01
  • 2010-09-28
相关资源
最近更新 更多