获取和设置 2D 数组对象值 (C++)

Getting and Setting 2D Array Object Values (C++)

本文关键字:C++ 对象 数组 设置 2D 获取      更新时间:2023-10-16

我是C++新手,2D 数组的工作方式让我感到困惑。我一直在网上阅读并试图了解导致我的特定问题的原因,但一无所获。


根据这个堆栈溢出答案,我应该能够通过这样做在我的 2D 数组中获取一个值:(*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];
                               ^  

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

主.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;
}

矩阵.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];
      }
    }  
};

矩阵.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
}

溶液:
对矩阵.h所做的更改:

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

对矩阵.cpp所做的更改:
makeArray()添加到构造函数。

  1. 正如 WhozCraig 和 Rakete1111 所说,您的问题是,当您的数据全部int时,您无法返回对double引用

但是即使在修复后,您也可能遇到其他问题。

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

还不是问题,但是。

  1. 您不需要任何行和列值的 setter。或者,您需要以这样一种方式更改这些资源库,以便重新分配新矩阵以适应新大小。

  2. #include是一回事,就像复制和粘贴命名文件的内容一样。而且你有一个用于uint的typedef,它只是从matrix.h复制粘贴到matrix.cpp和main.cpp,所以如果你不再次指定它,它甚至可以工作。

  3. 您有一个using namespace std,但没有包含 std 标头。您可能需要该内容,例如,如果您#include <iostream><vector>或任何其他标准库标头。或者,如果出于某种原因,您在自己的namespace std {...}块中编写了代码。

matrix[row][col]是正确的

方法,因为matrix是一个int**,应用两次operator[]就像两次引用指针,你会得到一个单独的int

为什么你得到一个错误,因为at返回一个double&。让我解释一下,matrix[row][col]返回一个int,并且这个int提升double。该double是一个临时变量,你不能从临时变量中引用,这就是编译器抱怨的原因。

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