-1

Im struggling to understand how I can declare an array in a class and use it within all functions in that same class. The arrays size is dependant on the user .

class Game{
 public:
    void createBoard();
    void gameStart();
    void inputBoard();
    void inputBoardSize();
    Game(int,int);


private:
    int rowChoice;
    int colChoice;
    int playerTurnRow;
    int playerTurnCol;
    string playerTurn;
    string board[rowChoice][colChoice];

};

Game::Game(int row,int col){
    rowChoice = row;
    colChoice = col;
    playerTurnRow = 0;
    playerTurnCol = 0;
    playerTurn = "R";
    board[row][col];
}

void Game::createBoard(){


        for (int arrayRow = 0;arrayRow < rowChoice;arrayRow++){
            for (int arrayCol = 0;arrayCol < colChoice;arrayCol++){
                board[arrayRow][arrayCol] = " ";
            }
}

My declaration might be wrong but any help would be appreciated

  • 3
    What is `board[row][col];` supposed to do?? You probably want a `std::vector>` instead of the raw array: `string board[rowChoice][colChoice];` – user0042 Aug 29 '17 at 07:11
  • Please just use `std::vector` and spare yourself a lot of time and pain – UnholySheep Aug 29 '17 at 07:12
  • 1
    A class definition specifies object layout. Object layout is a static unchangeable property. How is such an array, as you specified, supposed to be a fixed property of all the objects? – StoryTeller - Unslander Monica Aug 29 '17 at 07:13
  • If the user is supposed to determine the size at runtime, you'll not get away with a static array (that could be initialized using templates at compiletime) but a dynamic array that is heap allocated (i.e. using the new operator). As pointed out before, it is easier to use a dynamic stl array type like std::vector. – DrPepperJo Aug 29 '17 at 08:06

1 Answers1

0

Your syntax makes sense, but it is not how C++ works. A standard array can only be a fixed size (defined at compile time, not dynamically generated) unless you store that array on the heap.

The reason people are telling you to use an STL vector (from the C++ standard libary) is because you won't have to manage storing a standard 2d array on the heap, which means new and delete and the potential for memory leaks.

vectors also allow you to resize, do bounds checking for you, and are generally just as efficient because they store the array in the same way (contiguously in memory).

Here is what I mean:

#include <iostream>
#include <vector>
#include <algorithm>

using std::cin;
using std::cout;
using std::endl;
using std::vector;

void standard_array_example(int r, int c)
{
  // here you are responsible for managing memory yourself
  // allocate an array on the heap, r * c sized
  char *board = new char[r * c];

  // fill the board with x's
  std::fill_n(board, r * c, 'x');

  for (int i = 0; i < r; i++)
  {
    for (int k = 0; k < c; k++)
    {
      cout << board[i * c + k] << " ";
    }
    cout << endl;
  }

  // you must manually free your memory, or else there is a leak
  delete[] board;
}

void vector_example(int r, int c)
{
  // here, your memory is managed for you. when board goes out of scope
  // it is destroyed. the scope here is the end of the function
  vector<vector<char>> board;
  for (int i = 0; i < r; i++)
  {
    board.push_back(vector<char>(c, 'x'));
  }

  for (auto r : board)
  {
    for (auto c : r)
    {
      cout << c << " ";
    }
    cout << endl;
  }
}

int main()
{
  int r, c;
  cin >> r;
  cin >> c;

  standard_array_example(r, c);
  cout << endl;
  vector_example(r, c);

  system("PAUSE");

  return 0;
}

Both function the same:

5
5
x x x x x
x x x x x
x x x x x
x x x x x
x x x x x

x x x x x
x x x x x
x x x x x
x x x x x
x x x x x
Press any key to continue . . .
Josh
  • 12,602
  • 2
  • 41
  • 47