获取和设置 2D 数组对象值 (C++)
Getting and Setting 2D Array Object Values (C++)
我是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()
添加到构造函数。
- 正如 WhozCraig 和 Rakete1111 所说,您的问题是,当您的数据全部
int
时,您无法返回对double
的引用。
但是即使在修复后,您也可能遇到其他问题。
- 你永远不会调用你的makeArray函数和/或在你的构造函数中你永远不会分配(
new
)你的数组,就像你在makeArray中所做的那样。
还不是问题,但是。
-
您不需要任何行和列值的 setter。或者,您需要以这样一种方式更改这些资源库,以便重新分配新矩阵以适应新大小。
-
#include
是一回事,就像复制和粘贴命名文件的内容一样。而且你有一个用于uint的typedef,它只是从matrix.h复制粘贴到matrix.cpp和main.cpp,所以如果你不再次指定它,它甚至可以工作。 -
您有一个
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&
显然可以解决它:)
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 将对象移动到std::shared_ptr
- 代理对象的常量正确性
- 提升 ASIO 无法识别计时器对象
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 如何返回一个类的两个对象相加的结果
- 使用std::函数映射对象方法
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 构造对象的歧义
- 使用"std::unordereded_map"映射到"std::list"对象