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

查看:214
本文介绍了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.

另一个问题是InsertArray()函数,在那里我会把returnPiece()函数中的InsertArray()的说法。

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

与InsertArray()问题是争论,继承人的code吧:

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];
         }
    }
}

这里的问题是,它不接受我的returnPiece(),如果我删除了[4] [4],我的编译器不接受。

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. 返回整个pieceArray在returnPiece()
  2. 的正确语法在InsertArray参数()
  3. InsertArray()接受returnPiece()的参数

这3个都是我需要帮助的主要问题,并有同样的问题,当我尝试使用指针的指针的方法。有谁知道如何解决这些问题,3?

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?

推荐答案

在各地的传递你的数组,你必须决定是否要进行数组的一个副本,或者如果你只是想返回一个指针的阵列。对于返回数组,你不能(容易)返回副本 - 你只能返回一个指针(或引用C ++)。例如:

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.

您声明网​​:: InsertArray 是正确的 - 它需要一个参数,它是整数的4×4阵列。这是调用 - 值:当你调用它,你让你的4×4阵列的副本,所以您所做的任何修改都不会反映在通过阵列中如果你不是想用调用 - 参考,你可以指针传递给数组来代替:

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];
     }
}

这些类型声明的指针的多维数组可以得到真正的混乱很快。我建议做一个的typedef 它像这样:

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) { ... }

您也可以使用 CDECL 计划,以帮助破译复杂的C / C ++类型。

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

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

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