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

时间:2016-07-09 04:00:53

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

我是C ++的新手,2D阵列的工作方式令我感到困惑。我一直在网上阅读并试图了解是什么导致了我的具体问题,但却没有提出任何问题。

According to this Stack Overflow answer,我应该能够通过这样做来获取我的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];
                               ^  

以下是相关的完整(相关/简明)代码:

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
}

SOLUTION:
对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()添加到构造函数。

2 个答案:

答案 0 :(得分:2)

  1. 正如WhozCraig和Rakete1111所说,问题是,当您的数据全部为double时,您无法将参考返回int
  2. 但即使修好了这个问题,你也可能遇到其他问题。

    1. 你永远不会调用你的makeArray函数和/或在你的构造函数中你从不分配(new)你的数组,就像你在makeArray中那样。
    2. 还没问题,但是。

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

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

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

答案 1 :(得分:1)

matrix[row][col]是正确的方法,因为matrixint**,而应用operator[]两次就像引用指针两次,而你&# 39;获得个人int

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

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