C++ 返回并插入二维数组对象

发布于 2024-07-11 04:38:30 字数 1164 浏览 4 评论 0原文

我试图从一个较小的二维数组对象返回一个数组数据成员,并尝试将该数组插入到一个更大的二维数组对象中。 但当我尝试这样做时,我遇到了两个问题。

第一个问题是我想返回 2D 数组的名称,但我不知道如何正确的语法来返回 2D 数组名称。

这就是我的 2D 数组数据成员的样子

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

,我想将此数组返回到函数中,但这会导致编译器错误:

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

我厌倦了使用此返回类型,它起作用了:

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

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

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

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

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]”,我的编译器不会接受。

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

手心的海 2024-07-18 04:38:32

看来您需要阅读更多有关 C++ 中的指针以及按引用传递与按值传递的内容。

您的 returnPiece 方法定义为返回单个单元格的值。 给定索引(例如,[4][4]),您将返回该单元格内容的副本,因此您将无法更改它,或者更正确地说,更改它会更改副本。

我确信有人会给你正确的语法,但我真的建议你学习这些东西,否则你可能会使用你得到的不正确的代码。

It seems like you need to read up more on pointers in C++ and on pass by reference vs. pass by value.

Your returnPiece method is defined as returning the value of a single cell. Given the index (e.g., [4][4]) you return a copy of the contents of that cell, so you won't be able to change it, or more correctly, changing it would change the copy.

I'm sure someone will give you the correct syntax, but I would really recommend learning this stuff since otherwise you may use the code that you do get incorrectly.

也只是曾经 2024-07-18 04:38:32

我会这样做:

class Array {
  public:

    Array() {
      for (int i = 0; i < 4; ++i)
      {
         for (int j = 0; j < 4; ++j)
         {
            (*this)(i, j) = 0;
         }
      }
    }


    int &operator()(int i, int j)
    {
      return pieceArray[i][j];
    }

  private:
    int pieceArray[4][4];
};

然后你可以做类似的事情:

  Array x; // create 4x4 array
  x(1, 2) = 3; // modify element

Here is how I would do it:

class Array {
  public:

    Array() {
      for (int i = 0; i < 4; ++i)
      {
         for (int j = 0; j < 4; ++j)
         {
            (*this)(i, j) = 0;
         }
      }
    }


    int &operator()(int i, int j)
    {
      return pieceArray[i][j];
    }

  private:
    int pieceArray[4][4];
};

You can then do something like:

  Array x; // create 4x4 array
  x(1, 2) = 3; // modify element
无悔心 2024-07-18 04:38:32

Adam Rosenfield arrayPtr 存在问题,Piece::pieceArray 可能会从你的下面改变。 (按引用复制与按值复制。)

按值复制效率低下。 但如果你真的想这么做,那就作弊吧:

struct FOO { int  piece [4][4]; };

FOO Piece::returnPiece()
{
  FOO f;
  memcpy( f.piece, pieceArray, sizeof(pieceArray) );
  return f;
}

void Grid::InsertArray( const FOO & theFoo )
{
  // use theFoo.piece[i][j]
}

当然,更好、更面向对象的解决方案是让 returnPiece() 创建并返回一个 Piece数组对象。 (正如胡安建议的那样……)

Trouble with Adam Rosenfield's arrayPtr in that Piece::pieceArray can change out from under you. (Copy-by-reference vs Copy-by-value.)

Copy-by-value is inefficient. But if you really want to do it, just cheat:

struct FOO { int  piece [4][4]; };

FOO Piece::returnPiece()
{
  FOO f;
  memcpy( f.piece, pieceArray, sizeof(pieceArray) );
  return f;
}

void Grid::InsertArray( const FOO & theFoo )
{
  // use theFoo.piece[i][j]
}

Of course, a better, more Object-Oriented solution, would be to have returnPiece() create and return a Piece or Array object. (As Juan suggested...)

情仇皆在手 2024-07-18 04:38:31

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

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

要使用它,请像这样调用它:

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

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

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

// 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 ,如下所示:

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

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

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

您还可以使用 cdecl 程序帮助破译复杂的 C/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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文