C++将二维数组对象插入另一个二维数组对象

标签 c++ multidimensional-array

在使用 Dev C++ 时,我试图将一个较小的二维数组对象插入到一个较大的二维数组对象中。在尝试实现这一目标时,我遇到了不知道如何解决的编译器错误。

我尝试通过返回数组名称来插入较小的对象。然后我尝试用较小数组的值更改大数组中的值。

有两行代码我遇到了问题:

int result = smallerArray.extractPiece();
largerArray.extractArray(result);

在这两行代码中:

int Piece::extractPiece()
{
    return **pieceArray;
}

void Grid::extractArray( int** arr )
{
    for(int i = 0; i < xGrid ; ++i)
    {
         for (int j = 0; j < yGrid ; ++j)
         {
             squares[i][j] = arr[i][j];
                 }
    }
}

The two of the problems is that "int result" will not hold smallerArray.extractPiece(), and if i just put "smallerArray.extractPiece()" in largerArray.extractArray(), i still get problems. I attempted to make "int result" a pointer pointer, as "int** result", i still have the same errors.

These are the errors that i get when i try to compile in Dev C++:

In function `int main()';
invalid conversion from `int' to `int**'
initlizing argument 1 of 'void Grid::extractArray(int**)'
[Build Error] [grid test.o] Error 1

有人知道怎么回事吗?

最佳答案

正是这一堆代码:

int result = smallerArray.extractPiece();
largerArray.extractArray(result);
// ...
int Piece::extractPiece() {
    return **pieceArray;
}

尝试将一个 int 传递给 extractArray,它需要一个指向指针的指针,推测是您的动态数组,而不是一个 int。尝试将其更改为

int **result = smallerArray.extractPiece();
largerArray.extractArray(result);
// ...
int ** Piece::extractPiece() {
    return pieceArray;
}

仅将结果更改为指向指针的指针是行不通的。您当然还必须更改 extractPiece 返回的内容(从 int 更改为 int**)

关于C++将二维数组对象插入另一个二维数组对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/394763/

相关文章:

java - 访问m维数组中n维数组的索引(n<m)java

c - 如何在 C 中打印具有特定字符的单词?

c++ - 使用 std::system 将 bash(例如 ssh)转换为 C++

c++ - 将结构的一部分分配给c中的另一个结构

c++ - 如何找到将元素插入到 std::map 中的位置而不实际插入它

java - 通过迭代将字符串数组添加到二维字符串数组

c - C 中带有 printf 和多维数组的 7 段显示

javascript - 如何在 JavaScript 中访问二维数组的范围?

c++ - 创建在 C++ 结构体中声明的数组 a[1] 的多个实例

c++ - std::exception 的 What() 方法不是虚拟的?