C++ 返回和插入二维数组对象 [英] C++ Returning and Inserting a 2D array object

查看:33
本文介绍了C++ 返回和插入二维数组对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从一个较小的二维数组对象返回一个数组数据成员,并尝试将该数组插入到一个较大的二维数组对象中.但是在尝试这个时,我遇到了两个问题.

第一个问题是我想返回二维数组的名称,但我不知道如何正确使用语法来返回二维数组名称.

这就是我的二维数组数据成员的样子

<前>私人的:intpieceArray[4][4];//二维较小的数组

并且我想将这个数组返回到一个函数中,但是这个会导致编译器错误:

<前>int Piece::returnPiece(){返回块数组;//无效//返回二维数组名}

我厌倦了使用这种返回类型并且它有效:

<前>int Piece::returnPiece(){返回pieceArray[4][4];}

但我不确定这是否是我想要的,因为我想返回数组及其所有内容.

另一个问题是 InsertArray() 函数,我会将 returnPiece() 函数放在 InsertArray() 的参数中.

InsertArray() 的问题在于参数,这是它的代码:

<前>void Grid::InsertArray( int arr[4][4] )//编译器接受,但不工作{for(int i = 0; i

问题在于它不接受我的 returnPiece(),如果我删除[4][4]",我的编译器不接受.

大部分都是语法错误,但我该如何解决这些问题?

  1. 在 returnPiece() 中返回整个pieceArray
  2. InsertArray() 中参数的正确语法
  3. InsertArray() 接受 returnPiece() 的参数

这三个是我需要帮助的主要问题,当我尝试使用指针指针方法时也遇到了同样的问题.有谁知道如何解决这三个问题?

解决方案

在传递数组时,您必须决定是否要制作数组的副本,或者是否只想返回指向数组.对于返回数组,您不能(轻松)返回副本 - 您只能返回一个指针(或 C++ 中的引用).例如:

//Piece::returnPiece 是一个不带参数的函数,返回一个指向//4x4 整数数组int (*Piece::returnPiece(void))[4][4]{//返回指向数组的指针返回 &pieceArray;}

要使用它,请这样称呼它:

int (*arrayPtr)[4][4] = myPiece->returnPiece();int cell = (*arrayPtr)[i][j];//单元格现在存储第 (i,j) 个元素的内容

注意类型声明和使用之间的相似性 - 括号、解引用运算符 * 和括号位于相同的位置.

您对 Grid::InsertArray 的声明是正确的 - 它需要一个参数,即一个 4x4 整数数组.这是按值调用:每当你调用它时,你都会复制你的 4x4 数组,所以你所做的任何修改都不会反映在传入的数组中.如果你想使用按引用调用,你可以改为传递指向数组的指针:

//InsertArray 接受一个参数,它是一个指向 4x4 整数数组的指针void Grid::InsertArray(int (*arr)[4][4]){for(int i = 0; i < x_ROWS; i++){for(int j = 0; j 

这些带有指向多维数组的指针的类型声明很快就会变得非常混乱.我建议为它制作一个 typedef 像这样:

//声明 IntArray4x4Ptr 为指向 4x4 整数数组的指针typedef int (*IntArray4x4Ptr)[4][4];

然后你可以声明你的函数更具可读性:

IntArray4x4Ptr Piece::returnPiece(void) { ... }void Grid::InsertArray(IntArray4x4Ptr arr) { ... }

您还可以使用 cdecl 程序来提供帮助破译复杂的 C/C++ 类型.

I am trying to return an array Data Member from one smaller 2D Array Object, and trying to insert the array into a larger 2D array object. But when attempting this, I came into two problems.

First problem is that I want to return the name of the 2D array, but I do not know how to properly syntax to return 2D Array name.

This is what my 2D Array data member looks like

private:
int pieceArray[4][4];
// 2D Smaller Array

and I want to return this array into a function, but this one causes a compiler error:

int Piece::returnPiece()
{
    return pieceArray; //not vaild
    // return the 2D array name
}

I tired using this return type and it worked:

int Piece::returnPiece()
{
    return pieceArray[4][4];
}

But I am unsure if this is what I want, as I want to return the array and all of it's content.

The other problem is the InsertArray() function, where I would put the returnPiece() function in the InsertArray()'s argument.

The problem with the InsertArray() is the argument, heres the code for it:

void Grid::InsertArray( int arr[4][4] ) //Compiler accepts, but does not work
{
    for(int i = 0; i < x_ROWS ; ++i)
    {
         for (int j = 0; j < y_COLUMNS ; ++j)
         {
             squares[i][j] = arr[i][j];
         }
    }
}

The problem with this is that it does not accept my returnPiece(), and if i remove the "[4][4]", my compiler does not accept.

Mostly all these are syntax errors, but how do I solve these problems?

  1. Returning the whole pieceArray in returnPiece()
  2. The correct syntax for the argument in InsertArray()
  3. The argument of InsertArray() accepting the returnPiece()

These 3 are the major problems that I need help with, and had the same problem when I attempt to use the pointer pointer method. Does anyone know how to solve these 3 problems?

解决方案

When passing your array around, you have to decide whether or not you want to make a copy of the array, or if you just want to return a pointer to the array. For returning arrays, you can't (easily) return a copy - you can only return a pointer (or reference in C++). For example:

// Piece::returnPiece is a function taking no arguments and returning a pointer to a
// 4x4 array of integers
int (*Piece::returnPiece(void))[4][4]
{
    // return pointer to the array
    return &pieceArray;
}

To use it, call it like so:

int (*arrayPtr)[4][4] = myPiece->returnPiece();
int cell = (*arrayPtr)[i][j];  // cell now stores the contents of the (i,j)th element

Note the similarity between the type declaration and using it - the parentheses, dereferencing operator *, and brackets are in the same places.

Your declaration for Grid::InsertArray is correct - it takes one argument, which is a 4x4 array of integers. This is call-by-value: whenever you call it, you make a copy of your 4x4 array, so any modification you make are not reflected in the array passed in. If you instead wanted to use call-by-reference, you could pass a pointer to an array instead:

// InsertArray takes one argument which is a pointer to a 4x4 array of integers
void Grid::InsertArray(int (*arr)[4][4])
{
     for(int i = 0; i < x_ROWS; i++)
     {
         for(int j = 0; j < y_COLUMNS ; j++)
             squares[i][j] = (*arr)[i][j];
     }
}

These type declarations with pointers to multidimensional arrays can get really confusing fast. I recommend making a typedef for it like so:

// Declare IntArray4x4Ptr to be a pointer to a 4x4 array of ints
typedef int (*IntArray4x4Ptr)[4][4];

Then you can declare your functions much more readable:

IntArray4x4Ptr Piece::returnPiece(void) { ... }
void Grid::InsertArray(IntArray4x4Ptr arr) { ... }

You can also use the cdecl program to help decipher complicated C/C++ types.

这篇关于C++ 返回和插入二维数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆