How to access element of a 2D array located in a struct

Just to let you know, this is some sloppy coding, so please ignore the the ugliness of it. I need to access an element of a two dimensional array, that is located in struct Gameboard, just for comparison.
This is the function where the problem is:
1
2
3
4
5
6
7
8
9
10
bool position::isLegal(){
    char endspace = gameboard.board(pos.endPosition.i, pos.endPosition.j);
    if(pos.endPosition.i == pos.startPosition.i && pos.endPosition.j == pos.startPosition.j) //if start coords are the same as end coords
        return false;
    else
        if(endspace == ".")
            return true;
        else
            return false;
}


This is part of the struct position:
1
2
3
4
5
6
7
8
9
struct position{
    startmove startPosition;
    endmove endPosition;
    string sheepmove;

Gameboard gameboard;
.
.
.


and here is part of the struct Gameboard:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct Gameboard{

/*
2-diminensional array of characters
*/
char board[7][7] = {{' ', ' ', 'F', '.', 'F', ' ', ' '},
                    {' ', ' ', '.', '.', '.', ' ', ' '},
                    {'.', '.', '.', '.', '.', '.', '.'},
                    {'S', 'S', 'S', 'S', 'S', 'S', 'S'},
                    {'S', 'S', 'S', 'S', 'S', 'S', 'S'},
                    {' ', ' ', 'S', 'S', 'S', ' ', ' '},
                    {' ', ' ', 'S', 'S', 'S', ' ', ' '}
                   };

.
.
.


This is a VERY small part of a large, sloppy program. I just need to get over this hurtle. Thank you in advance for help.
1
2
3
4
5
6
7
8
for ( size_t i = 0; i < 7; i++ )
{
   for ( size_t j = 0; j < 7; j++ )
   {
      std::cout << pos.gameboard.board[i][j];
   }
   std::cout << std::endl;
}
I thought since it was in a position function, I wouldn't need to specify pos.gameboard...... I tried it though and either way there is an error, the error is 'pos.position::gameboard.Gameboard::board' cannot be used as a function. without the pos, the error is '((position*)this)->position::gameboard.Gameboard::board' cannot be used as a function
Or, another option I could use help with is how to use a two dimensional char array as a parameter. Then I could have direct access to the array and I need to access it in other functions as well.
To acces an element in an array, you have to use [].
The way you're using it now, you're calling a function called board(int, int), not the xth element of the yth row of the member board.

So:
char endspace = gameboard.board(pos.endPosition.i, pos.endPosition.j);
Has to become:
char endspace = gameboard.board[pos.endPosition.i][pos.endPosition.j];
Last edited on
Thanks, I got it now.
Topic archived. No new replies allowed.