0

i'm having some trouble on initializing an empty array ,or so i call it, in a class. basically i want to fill a 2d array with 0.

here's my code:

Board.cpp

#include "Board.h"
#include <string>
#include <iostream>
#include "Player.h"

using namespace std;

Board::Board()
: _board{ { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } }
{
}

void Board::drawBoard()
{
    for (int i = 0; i < 4; i++){
        for (int j = 0; j < 4; j++){
            if (i == 0 && j == 0){
                cout << " ";
            }
            else if (i == 0){
                cout << " " << j << " ";
            }
            else if (j == 0){
                cout << i;
            }
            else {
                if (_board[i][j] == 0)
                    cout << "| " << "|";
                else
                    cout << "|" << _board[i][j] << "|";
            }
        }
        cout << endl;
    }
}

void Board::playerInput(char input)
{
}

here is Board.h:

#pragma once
#include <string>
#include <iostream>

class Board
{
public:
    Board();
    void drawBoard();
    void playerInput(char input);


private:
    char _board[3][3];
    char _input;
};

is that a right way to initialize an array? i read some articles but it just confuses me more. i'm sorry if i sound stupid. but i need your help. thank you.

GroniumArgor
  • 103
  • 1
  • 11

3 Answers3

2

All you need to do is value initialize the data member:

Board::Board() : _board() {}

This will zero-initialize all the elements of _board, and also works for C++03.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

The method juanchopanza is using is the current set by c++11 However, I would suggest some small changes. If all the default constructor is doing is initializing the array I would instead initialize it in the header file. Like this

#pragma once
#include <string>
#include <iostream>

class Board
{
public:
    Board()=default; //becomes compiler constructed 
    void drawBoard();
    void playerInput(char input);


private:
    char _board[3][3]{};//added {} here
    char _input;
};

This way the array is initialized BEFORE the constructor is called and the constructor is made by the compiler. The advantage here is that there is less code needed by you and it is scalable for additional constructor if you desire.

for example:

if you add

Board(char input) for input all that is needed is:

Board(char input)
{
   _input=input;// no need to repeat initialization code for _board array

}
  • 1
    +1, that is actually a good idea (but I doubt OP's compiler supports this.) – juanchopanza Aug 31 '14 at 11:00
  • Both the OP's code and juan's code initialize the array at the same point in the program's execution as this code. – chris Aug 31 '14 at 11:20
  • What is OP? And yes " but I doubt OP's compiler supports this " is true many compilers are yet to have fully implemented c++11 yet. –  Aug 31 '14 at 19:14
0

Depending on your compiler, you should be able to do this (notice use of parenthesis instead of brackets):

Board::Board()
: _board( { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } )
{
}

Ref: https://stackoverflow.com/a/2409882/1932930

Looks like you are using VS2013. It looks like VS2013 supports it. Ref: http://wiki.apache.org/stdcxx/C%2B%2B0xCompilerSupport

Community
  • 1
  • 1
tdemay
  • 649
  • 8
  • 23
  • yes i'm using vs2013 it gives me 4 errors: Error 1 error C2536: 'Board::Board::_board' : cannot specify explicit initializer for arrays 2 IntelliSense: only '()' is allowed as initializer for array member "Board::_board" 3 IntelliSense: expected a ';' Project1 4 IntelliSense: expected a declaration – GroniumArgor Aug 31 '14 at 06:34