在数组中的简单代码中出现错误

error in simple code in in with array

本文关键字:代码 错误 简单 数组      更新时间:2023-10-16

我有一段代码

class MazeClass{
    public:
        void printMaze(){
            for (int y=0; y<N;y++){
                cout <<Map[y]<<endl;
            }
        }
        void moveMe(){
            if (GetAsyncKeyState(VK_UP) !=0){
                if ((Map[myLocation[0]][myLocation[1]-1])) ==' '){
                    Map[myLocation[0]][myLocation[1]]) =' ';
                    Map[myLocation[0]][myLocation[1]-1]) ='@';
                    myLocation[1]--;
                }
            }
        }
    private:
        char Map [N][N+1] =     {"##########",
                                "#@       #",
                                "#        #",
                                "#        #",
                                "#        #",
                                "#        #",
                                "#        #",
                                "#        #",
                                "#        #",
                                "##########"};
        int myLocation[2]={1,1};
};

,当我试图编译它时,它给了我一个错误:

F: c++ Mazemain.cpp|17|error: '==' token之前的primary-expression

F: c++ main.cpp迷宫| 17 |错误:预计";")"牌|

第17行

if ((Map[myLocation[0]][myLocation[1]-1])) ==' '){

我真的希望你们能帮忙,我被困在这个问题上一个多小时了

如果使用括号有问题,这里是ok变体:

   void moveMe(){
      if (GetAsyncKeyState(VK_UP)!=0){
         if ((Map[myLocation[0]][myLocation[1]-1]) ==' ')
         {
            Map[myLocation[0]][myLocation[1]] =' ';
            Map[myLocation[0]][myLocation[1]-1] ='@';
            myLocation[1]--;
         }
      }
   }

此外,您不能像这样初始化类成员:

private:
    char Map [N][N+1] =     {"##########",
                            "#@       #",
                            "#        #",
                            "#        #",
                            "#        #",
                            "#        #",
                            "#        #",
                            "#        #",
                            "#        #",
                            "##########"};
    int myLocation[2]={1,1};

在类中,你只能声明它们,然后再初始化,例如在调用构造函数时。

if ((Map[myLocation[0]][myLocation[1]-1])) ==' ')改为:

 if ((Map[myLocation[0]][myLocation[1]-1]) ==' '){

你有一个额外的右括号.

编译器错误在这里帮助你,学会阅读它们:

你有:

error: expected primary-expression before '==' token| F:C++Mazemain.cpp|17|error: expected ';' before ')

查看第17行及其前后的行并查找错误(如果是简单的错误,例如语法,您应该可以用这个解决它)。更复杂的错误可以通过调试器解决。

还有,你使用的是哪种编辑器,因为大多数编辑器都有突出显示匹配括号的功能,当你位于另一个括号上时,从而避免了这个问题。

你的样式导致可读性问题。
我稍微重写了一下,以显示更好的风格

#include <array>
template<int N>
class MazeClass
{
    typedef array<char,N> Line;
    typedef array<Line,N> Map;
    const char clearchar = ' ';
    const char wallchar  = '#';
    const char dudechar  = '@';
    struct Pos { int x,y; };
    void moveTo(int dx,int dy)
    {
        int tox = mypos.x+dx;
        int toy = mypos.y+dy;
        if ( map[tox][toy] == clearchar )
        {
            map[mypos.x][mypos.y] = clearchar; // clear old
            map[tox][toy] = dudechar;          // set new
            mypos = Pos{ tox,toy };            // update pos
        }
    }
public:
    MazeClass()
    {
        for( auto& l : map )
            for( auto& c : l )
                c=clearchar;
        for(int i=0;i<N;++i)
        {
            map [0]   [i]   = wallchar;
            map [N-1] [i]   = wallchar;
            map [i]   [0]   = wallchar;
            map [i]   [N-1] = wallchar;
        }
        map[mypos.x][mypos.y] = dudechar ;
    }
    void printMaze()
    {
        for( auto& l : map )
        {
            for( auto& c : l )
                cout << c;
            cout << endl;
        }
    }
    void moveMe()
    {
        if ( GetAsyncKeyState(VK_UP) )  moveTo(0,-1);
        // etc
    }
private:
    Map map;
    Pos mypos = Pos{1,1};
};
void tryit()
{
    MazeClass<10> maze;
    maze.printMaze();
    maze.moveMe();
}