c++ class: 在 public 函数中使用私有数组?

c++ class: using a private array in a public function?

这是我在头文件中使用的代码:

#ifndef TETRIS_TETRIMINO
#define TETRIS_TETRIMINO

const int TETRIMINO_GRID_SIZE = 4;

struct Location {
    int row;
    int col;
};

class Tetrimino {
private:
    int grid[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE];
    char color;
    Location location;
public:
    // constructor
    Tetrimino(int type = 7); // valid type values are 0-6

//---------------------------------------------
//accessors
    char getColor();
    Location getLocation();
    void getGrid(int gridOut[][TETRIMINO_GRID_SIZE]);

//---------------------------------------------
//mutators
    void setLocation(Location newLocation);
    void setLocation(int row, int col);

    void rotateLeft();
    void rotateRight();
    void moveLeft();
    void moveRight();
    void moveDown();
    void moveUp();

//---------------------------------------------
//others
    void dataDump();
    };
    #endif

这是.cpp:

#include "tetrimino.h"
#include <iostream>
#include <ctime>
using namespace std;

//random number generator
int randNum()
{
    int randNum;
    int high = 6;
    int low = 0;
    srand(static_cast<unsigned int>(time(NULL)));
    randNum = rand() % (high - low + 1) + low;
    return randNum;

}

Tetrimino::Tetrimino(int type)
{
    //check to see if type is 0-6, if not set to 7
    if (type < 0 || type >= 7)
    {
        type = randNum();
    }

    //set associated type to a tetrimino
    if (type == 0)
    {
        //set grid to i tetro
        int grid[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE] =
        {
            { 0, 0, 0, 0 },
            { 1, 1, 1, 1 },
            { 0, 0, 0, 0 },
            { 0, 0, 0, 0 }
        };
        //set color to teal
        color = 't';

        //initialize starting position
        location.row = 0;
        location.col = 0;
    }
    else if (type == 1)
    {
        //set grid to j tetro
        int grid[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE] =
        {
            { 0, 0, 0, 0 },
            { 0, 1, 0, 0 },
            { 0, 1, 1, 1 },
            { 0, 0, 0, 0 }
        };
        //set color to blue
        color = 'b';

        //initialize starting position
        location.row = 0;
        location.col = 0;
    }
    else if (type == 2)
    {
        //set grid to L tetro
        int grid[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE] =
        {
            { 0, 0, 0, 0 },
            { 0, 0, 1, 0 },
            { 1, 1, 1, 0 },
            { 0, 0, 0, 0 }
        };
        //set color to orange
        color = 'o';

        //initialize starting position
        location.row = 0;
        location.col = 0;
    }
    else if (type == 3)
    {
        //set grid to o tetro
        int grid[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE] =
        {
            { 0, 0, 0, 0 },
            { 0, 1, 1, 0 },
            { 0, 1, 1, 0 },
            { 0, 0, 0, 0 }
        };
        //set color to yellow
        color = 'y';

        //initialize starting position
        location.row = 0;
        location.col = 0;
    }
    else if (type == 4)
    {
        //set grid to s tetro
        int grid[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE] =
        {
            { 0, 0, 0, 0 },
            { 0, 1, 1, 0 },
            { 1, 1, 0, 0 },
            { 0, 0, 0, 0 }
         };
        //set color to green
        color = 'g';

        //initialize starting position
        location.row = 0;
        location.col = 0;
    }
    else if (type == 5)
    {
        //set grid to T tetro
        int grid[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE] =
        {
            { 0, 0, 0, 0 },
            { 0, 1, 0, 0 },
            { 1, 1, 1, 0 },
            { 0, 0, 0, 0 }
        };
        //set color to purple
        color = 'p';

        //initialize starting position
        location.row = 0;
        location.col = 0;
    }
    else if (type == 6)
{
    //set grid to z tetro
    int grid[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE] =
    {
        { 0, 0, 0, 0 },
        { 0, 1, 1, 0 },
        { 0, 0, 1, 1 },
        { 0, 0, 0, 0 }
    };
    //set color to red
    color = 'r';

    //initialize starting position
    location.row = 0;
    location.col = 0;
}
  };

  //accessors
  char Tetrimino::getColor()
  {
      return color;
  }
  Location Tetrimino::getLocation()
  {
      return location;
  }
  void Tetrimino::getGrid(int gridOut[][TETRIMINO_GRID_SIZE])
  {
      //loop goes through each row
      for (int row = 0; row < TETRIMINO_GRID_SIZE; row++)
      {
          //goes through each col of current row
          for (int column = 0; column < TETRIMINO_GRID_SIZE; column++)
          {
              cout << gridOut[row][column] << " ";
          }
          //new line between rows
          cout << endl;
      }
  }

//mutators
//leaving these out of this for sanity

  void main()
  {
      Tetrimino test(0);

      cout << test.getColor() << endl;
      test.getGrid(test.grid);

  }

好的,显然代码不完整。我对如何使用 public 函数 getGrid 从 Tetrimino class 打印出网格数组感到非常困惑和困惑。头文件是预先制作好的(虽然我理解)所以我不想编辑它。为什么 getGrid 函数首先需要一个参数?

我不能像在 main() 中尝试的那样简单地调用我想要打印的网格,因为它是私有的。我只是真的.. 是的,我知道这是错误的,但我不知道如何去做正确的事。

EDIT/UPDATE: 我从 getGrid() 中删除了参数,并将函数中的 gridOut 更改为简单的网格。但是,当我使用 test.getGrid() 调用函数时,打印的数组是:

-858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460

我修改的代码是:

void Tetrimino::getGrid()
{
    //loop goes through each row
    for (int row = 0; row < TETRIMINO_GRID_SIZE; row++)
    {
        //goes through each col of current row
        for (int column = 0; column < TETRIMINO_GRID_SIZE; column++)
        {
            cout << grid[row][column] << " ";
        }
        //new line between rows
        cout << endl;
    }
}

getGrid 已更改为 grid。

我现在这样调用函数:

void main()
{
    Tetrimino test(0);

    test.getGrid();

}

那个方法根本就错了,本来是打印一个参数的,不是内部数据,去掉输入参数,用内部矩阵就可以了。

顺便说一句,你不应该使用 std::endl 只打印 \n,直接使用 \n。 std::endl 将刷新 fd 并花费更多时间。

不要从你的 main 调用你的私有成员,也不要从你的 main 发送任何参数。这是您的成员,您不需要任何打印出来,您只需使用它,如果您对直接使用它感到困惑,请在您的函数

中使用 this 关键字
for (int row = 0; row < TETRIMINO_GRID_SIZE; row++)
  {
      //goes through each col of current row
      for (int column = 0; column < TETRIMINO_GRID_SIZE; column++)
      {
          cout << this->grid[row][column] << " ";
      }
      //new line between rows
      cout << endl;
  }

正如您所说,头文件已提供给您,我猜这是一项作业。 getGrid 方法旨在为外部调用者提供接口,以获取 Tetrmino 对象网格的副本。由于您不能 return 来自函数的数组,getGrid 方法提供了一个输出参数。

用法示例:

void Tetrimino::getGrid(int gridOut[][TETRIMINO_GRID_SIZE]) {
   for(int i = 0; i < TETRMINO_GRID_SIZE; i++) { 
      for(int j = 0; j < TETRMINO_GRID_IZE; j++ ) { 
          gridOut[i][j] = grid[i][j];
      }
   }
}

...
...

Tetrmino obj(3);

... 
... 

int grid[TETRIMINO_GRID_SIZE][TETRMINO_GRID_SIZE];
obj.getGrid(grid);
// now grid holds the copy of interal grid
for(int i = 0; i < TETRMINO_GRID_SIZE; i++) { 
   for(int j = 0; j < TETRMINO_GRID_IZE; j++ ) { 
       std::cout << grid[i][j] << " ";
   }
   std::cout << "\n";
}
std::cout << std::flush;

编辑: 扩展答案:为什么没有分配网格?

问题是,在您的构造函数中,您声明了一个与 class 成员同名的新 int 数组。这意味着,您没有初始化成员变量。 C++ 不允许在初始化后分配给原始数组,你只能复制。

将新变量更改为 gridNew 或类似的变量,然后逐个元素从 gridNew 复制到 grid 就像您现在从 grid 复制到 gridOutgetGrid 方法中。