Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;类:在公共函数中使用私有数组?_C++_Arrays_Class_Private_Public - Fatal编程技术网

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

C++ c++;类:在公共函数中使用私有数组?,c++,arrays,class,private,public,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;

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

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

  }
我更改的代码是:

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

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


顺便说一句,您不应该使用std::endl仅打印\n,请直接使用\n。std::endl将刷新fd并占用更多时间。

不要从main调用您的私有成员,也不要从main发送任何参数。它是您的成员,您不需要任何东西来打印它,您只需使用它,如果您混淆了直接使用它,请将它与函数中的
关键字一起使用

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;
  }
for(int row=0;rowcout grid[row][column]正如您所提到的,头文件是提供给您的,我想这是一个赋值。
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;
void Tetrimino::getGrid(int gridOut[][Tetrimino\u GRID\u SIZE]){
对于(inti=0;istd::cout“我不想编辑它”-如果你想打印私有成员,恐怕你必须这样做。我不能说为什么函数是这样写的,但是它不能在外部用于打印类的成员数组。话虽如此,
dataDump
做什么?
getGrid
可以用来打印任何
grid
。所以这个函数on不应是类的成员。如果此函数仅用于打印内部
网格
,则不需要在all@MikeSeymourdataDump只是一个函数,我计划用它来打印内部变量,以进行测试,但我还没有创建它即使我不想编辑,也要编辑它。这对我来说毫无意义,为什么一开始就有这样一个论点。“我对如何从Tetrimino类打印网格数组感到非常困惑和困惑”-我想你会更加“困惑”关于为什么您的成员网格仍然不确定,以及所有那些本地作用域自动
grid
声明都不会改变这一点。使用启动的警告进行编译应该会显示“未使用变量'grid'的七个不同实例”或者是这样的话。@WhozCraig是的,我现在在那里。我甚至更不明白为什么会发生这种情况。我的大脑正在努力找出为什么会发生这种情况,但我明白你指出的。直接使用是什么意思?你能详细说明一下我将如何使用\n而不是endl吗?@Zearia write
std::你说的对吗如果我理解正确的话,在这个解决方案中,getGrid的公共函数只存在于提取网格信息的过程中?
getGrid
getColor
的方法相同。唯一的区别是,因为使用了原始数组,所以返回副本就不那么简单了。啊,我明白了。谢谢你的帮助解释!现在,你不会碰巧知道为什么网格没有被设置为正确的值吧?我扩展了答案谢谢!非常好的解释,你反应很快!
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;
  }
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;