0

I have this little pieace of code

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

and when I try to compile it, it gives me an error :

F:\C++\Maze\main.cpp|17|error: expected primary-expression before '==' token

F:\C++\Maze\main.cpp|17|error: expected ';' before ')' token|

the line 17 is

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

I really hope you guys can help, I'm stuck on this for like over an hour.

1
  • 1
    I have voted to close as this is a question based on a basic typo (syntax error with parentheses) Commented Aug 15, 2014 at 9:30

3 Answers 3

1

you have trouble with brackets, here is ok variant:

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

besides, you can't initialize class member like this:

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

in class you can only declare them, and initialize later, e.g. when call constructor.

Sign up to request clarification or add additional context in comments.

Comments

1

Change if ((Map[myLocation[0]][myLocation[1]-1])) ==' ') to:

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

You had one extra closing bracket .

The compiler errors are here to help you, learn to read them:

You had:

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

So you look at line 17 and the lines before and after it and look for the error (if it is a simpler one, like syntax you should be able to solve it just with this). More complicated errors are solved with debuggers.

And also, what kind of editor are you using because most editors have the feature to highlight the matching bracket when you are positioned on the other thus avoiding this problem.

Comments

0

your style causes readability problem.
i rewrote it a little, to show a better style

#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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.