Dont know how to fix: expected primary-expression before ‘.’ token

I’m currently just starting off coding in c++, the current project I’m working on is making an in-console chess engine type thing. The function below is supposed to check if a particular square has a piece on it, and if so, if the piece is white. Board.grid is a 2D array of strings where " " would be an empty square, "wR1" one of the white rooks, "bQ1" the black queen, etcetera. I got the error expected primary-expression before '.' token on both if statements inside the functions and think it’s got to do with the .at() function called on the ‘square string’, but have no clue why this would give an error or what could be done to resolve it.

I would appreciate it greatly if anyone could help me with this. Thanks in advance!

#include <iostream>

using namespace std;

struct Board {
    string Grid[8][8] =
    {
        {"bR1", "bN1", "bB1", "bQ1", "bK1", "bB2", "bN2", "bR2"},
        {"bp1", "bp2", "bp3", "bp4", "bp5", "bp6", "bp7", "bp8"},
        {"   ", "   ", "   ", "   ", "   ", "   ", "   ", "   "},
        {"   ", "   ", "   ", "   ", "   ", "   ", "   ", "   "},
        {"   ", "   ", "   ", "   ", "   ", "   ", "   ", "   "},
        {"   ", "   ", "   ", "   ", "   ", "   ", "   ", "   "},
        {"wp1", "wp2", "wp3", "wp4", "wp5", "wp6", "wp7", "wp8"},
        {"wR1", "wN1", "wB1", "wQ1", "wK1", "wB2", "wN2", "wR2"},
    };
};


class Piece
    {
    public:
        bool getSquare (int square[2])
        {
            bool isOccupied = false;
            bool isWhite = false;
            if (Board.grid[square[1]][square[0]].at(0) != ' '){isOccupied = true;};
            if (Board.grid[square[1]][square[0]].at(0) == 'w'){isWhite = true;};
            bool arr[2] = {isOccupied, isWhite};
            return arr;
        };

    };

Answer

The symbol Board is a type not an instance. In C++ you can’t use the . operator to access type members, only members of objects.

The simple solution is to create a Board objects:

struct /* Anonymous structure */ {
    string Grid[8][8] =
    {
        {"bR1", "bN1", "bB1", "bQ1", "bK1", "bB2", "bN2", "bR2"},
        {"bp1", "bp2", "bp3", "bp4", "bp5", "bp6", "bp7", "bp8"},
        {"   ", "   ", "   ", "   ", "   ", "   ", "   ", "   "},
        {"   ", "   ", "   ", "   ", "   ", "   ", "   ", "   "},
        {"   ", "   ", "   ", "   ", "   ", "   ", "   ", "   "},
        {"   ", "   ", "   ", "   ", "   ", "   ", "   ", "   "},
        {"wp1", "wp2", "wp3", "wp4", "wp5", "wp6", "wp7", "wp8"},
        {"wR1", "wN1", "wB1", "wQ1", "wK1", "wB2", "wN2", "wR2"},
    };
} Board;  // Define the Board structure object instance

Or possibly define Board as a namespace, and use the scope operator:

namespace Board {
    string Grid[8][8] =
    {
        {"bR1", "bN1", "bB1", "bQ1", "bK1", "bB2", "bN2", "bR2"},
        {"bp1", "bp2", "bp3", "bp4", "bp5", "bp6", "bp7", "bp8"},
        {"   ", "   ", "   ", "   ", "   ", "   ", "   ", "   "},
        {"   ", "   ", "   ", "   ", "   ", "   ", "   ", "   "},
        {"   ", "   ", "   ", "   ", "   ", "   ", "   ", "   "},
        {"   ", "   ", "   ", "   ", "   ", "   ", "   ", "   "},
        {"wp1", "wp2", "wp3", "wp4", "wp5", "wp6", "wp7", "wp8"},
        {"wR1", "wN1", "wB1", "wQ1", "wK1", "wB2", "wN2", "wR2"},
    };
}

// ...

Board::grid[square[1]][square[0]].at(0)
//   ^^
// Note use of scope operator :: here