Initializing Array from a Class

Hello,

I'm taking an introductory C++ class and I'm still a little confused by the act of separating classes and functions. I can't figure out why I can't initialize an array from my .hpp file in my .cpp file. I get a cannot convert '<brace enclosed initializer list>' to 'char' error when I try to compile this section.

class FBoard
{
private:
char gameBoard[8][8]; //2D array with 8 rows & columns

...
};

FBoard::FBoard()
{
//initialize array to empty
gameBoard[8][8] = {{'e','e','e','e','e','e','e','e'}, {'e','e','e','e','e','e','e','e'}};

What am I missing?

Thanks!

While I don't "like" this style of initialization myself (it's ok for study and various special use cases), this is what you're thinking of doing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17



class FBoard
{
private:
char gameBoard[8][8]= {{'e','e','e','e','e','e','e','e'}, {'e','e','e','e','e','e','e','e'}}; //2D array with 8 rows & columns

public:
 FBoard();

};

FBoard::FBoard()
{
 // nothing needed here
}
Last edited on
That's what I initially wanted to do as well... the assignment I'm working on has a weird specification that I have to initialize the array in the default constructor somehow?

"Write a class named FBoard for playing a game... It should have:

An 8x8 array of char for tracking the positions of the pieces.
...
A default constructor that initializes the array to empty (you can use whatever character you want to represent empty). It should then put four o pieces on row 7, in columns 0, 2, 4, and 6. It should put an x piece on row 0, column 3. It should also initialize the other data members.
"
If the initialisation must be done with a member initialiser list:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>

struct FBoard
{
    // It should have an 8x8 array of char for tracking the positions of the pieces.
    char board[8][8] ;

    static constexpr char empty = '.' ;
    static constexpr char o_piece = 'O' ;
    static constexpr char x_piece = 'X' ;

    // A default constructor that initializes the array to empty
    // you can use whatever character you want to represent empty
    // member initializer list: https://en.cppreference.com/w/cpp/language/initializer_list
    FBoard() : board { { empty, empty, empty, empty, empty, empty, empty, empty },
                       { empty, empty, empty, empty, empty, empty, empty, empty },
                       { empty, empty, empty, empty, empty, empty, empty, empty },
                       { empty, empty, empty, empty, empty, empty, empty, empty },
                       { empty, empty, empty, empty, empty, empty, empty, empty },
                       { empty, empty, empty, empty, empty, empty, empty, empty },
                       { empty, empty, empty, empty, empty, empty, empty, empty },
                       { empty, empty, empty, empty, empty, empty, empty, empty } }
    {
        // It should then put four o pieces on row 7, in columns 0, 2, 4, and 6.
        board[7][0] = board[7][2] = board[7][4] = board[7][6] = o_piece ;

        // It should put an x piece on row 0, column 3.
        board[0][3] = x_piece ;
    }

    std::ostream& print( std::ostream& stm ) const
    {
        for( const auto& row : board )
        {
            for( char c : row ) std::cout << c << ' ' ;
            std::cout << '\n' ;
        }

        return stm ;
    }

    friend std::ostream& operator<< ( std::ostream& stm, const FBoard& brd )
    { return brd.print(stm) ; }
};

int main()
{
    const FBoard my_board ;
    std::cout << my_board << '\n' ;
}

http://coliru.stacked-crooked.com/a/e742e11369c1d286
Oh I see what I did wrong now-- thank you so much!
Topic archived. No new replies allowed.