简体   繁体   English

不知道如何解决:“。”之前的预期主表达式令牌

[英]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.我目前刚刚开始在 c++ 中进行编码,我正在从事的当前项目是制作控制台内国际象棋引擎类型的东西。 The function below is supposed to check if a particular square has a piece on it, and if so, if the piece is white.下面的 function 应该检查一个特定的正方形是否有一块,如果有,如果是白色的。 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. Board.grid是一个二维字符串数组,其中" "是一个空方块, "wR1"是白车之一, "bQ1"是黑皇后,等等。 I got the error expected primary-expression before '.' tokenexpected primary-expression before '.' token 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.函数内的两个 if 语句上的expected primary-expression before '.' token ,并认为它与在“方字符串”上调用的 .at .at() function 有关,但不知道为什么这会给出错误或可以采取什么措施来解决它。

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;
        };

    };

The symbol Board is a type not an instance.符号Board是一个类型而不是一个实例。 In C++ you can't use the .在 C++ 中,您不能使用. operator to access type members, only members of objects.运算符访问类型成员,只有对象的成员。

The simple solution is to create a Board objects:简单的解决方案是创建一个Board对象:

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:或者可能将Board定义为命名空间,并使用 scope 运算符:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM