C++ 类:在公共函数中使用私有数组

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

本文关键字:数组 函数 C++      更新时间:2023-10-16

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

#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);
  }

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

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

编辑/更新:我从getGrid()中删除了参数,并在函数中将gridOut更改为简单的grid。但是,当我使用 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();
}

正如你提到的,头文件是提供给你的,我想,这是一个赋值。 getGrid方法旨在为外部调用方提供接口,以获取 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元素,就像您现在在getGrid方法中从grid复制到gridOut一样。

这种方法是完全错误的,它被设计为打印一个参数而不是内部数据,只需删除输入参数并使用内部矩阵。

顺便说一句,您不应该使用 std::endl 只打印

,直接使用 。 std::endl 将刷新 fd 并花费更多时间。

不要从主节点调用您的私人成员,也不要从您的主服务器发送任何参数。它是您的成员,您不需要任何东西来打印出来,您只需使用它,如果您与直接使用它混淆,请在函数中将其与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;
  }