c++ - 获取和设置二维数组对象值 (C++)

标签 c++ arrays object matrix multidimensional-array

我是 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;
}

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

解决方案:
对 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()

最佳答案

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

但即使在修复之后您可能还会遇到其他问题。

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

还不是问题,但是。

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

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

  3. 你有一个 using namespace std ,但不包括标准 header 。你可能需要那个东西,例如如果你#include <iostream><vector>或任何其他标准库头文件。或者,如果出于某种原因您自己编写了代码 namespace std {...}阻止。

关于c++ - 获取和设置二维数组对象值 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38278176/

相关文章:

c++ - 链接 UnitTest++ 失败

c - 二维字符串数组和 Strcpy 错误?

javascript - 使用reduce将源对象的可枚举属性扩展到目标对象

javascript - 为什么我的数组要重组?

c++ - c++中对象的初始化

c++ - 根据大小扣除类型 C++

c++ - 移动 QMessageBox?

java - 这是什么文档格式?

javascript - 如何停止我的尾随线导致对象因数组大小而减慢

c++ - 即使检查了两个索引的边界,动态二维数组也存在奇怪的访问冲突