C++ 类 : using a private array in a public function?

标签 c++ arrays class private public

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

#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 类打印出网格数组感到非常困惑和困惑。头文件是预先制作好的(虽然我理解)所以我不想编辑它。为什么 getGrid 函数首先需要一个参数?

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

编辑/更新: 我从 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 已更改为网格。

我现在这样调用函数:

void main()
{
    Tetrimino test(0);

    test.getGrid();

}

最佳答案

正如您提到的,头文件已提供给您,我猜这是一项作业。 getGrid 方法旨在为外部调用者提供接口(interface)以获取 Tetrmino 对象网格的拷贝。由于您无法从函数返回数组,因此 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;

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

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

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

关于C++ 类 : using a private array in a public function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28293366/

相关文章:

c++ - 使用 C++ 中的 try 和 catch 方法创建一个函数以获取用户名

python - 如何在Python中比较两个数组并找到最佳匹配?

c++ - 程序员何时使用空基优化 (EBO)

c++ - 从外部函数/类设置动态数组的新大小?

C++ 不能使用另一个文件中的类

C++ 类设计建议

c++ - 我如何输出二维数组 C++ 的行总和

c++ - 在这种情况下有什么正确的方法来实现锁定?

c++ - 为什么我在使用 Boost UUID 时会收到来自 Valgrind 的未初始化值警告?

CUDA 数组缩减为元素总和。如何从设备向主机传达答案并打印?