0

我在处理以下错误时遇到问题:

c2660: c:\users\lukas plaehn\desktop\oop\sokoban\sokoban\main.cpp(126): 错误 C2660: 'CPlayer::moveUp' : 函数不接受 1 个参数

c2061:c:\users\lukas plaehn\desktop\oop\sokoban\sokoban\player.h(10):错误 C2061:语法错误:标识符 'CMaze'

我想要做的是在头文件中将对象作为参数传递。我也尝试过通过引用来做,但它不起作用。不知何故,编译器不允许我CMaze在头文件中声明任何类型的内容CPlayer

    class CPlayer:public CBlocks
    {
    public:
    CMaze * m;
    CPlayer(void);
    CPlayer(char sym, int x, int y, CMaze mz);
    ~CPlayer(void);
    void moveUp(CMaze maze);
    void moveDown(CMaze maze);
    void moveLeft(CMaze maze);
    void moveRight(CMaze maze);
};

    #include "Player.h"
    #include "Maze.h"
    //#include "headers.h"




    CPlayer::CPlayer(void)
    {
    }

    CPlayer::CPlayer(char sym, int x, int y, CMaze mz)
    {
    m = &mz;
    m_symbol = sym;
    m_x = x;
    m_y = y;
    }

    CPlayer::~CPlayer(void)
    {
    }



    void CPlayer::moveDown(CMaze maze)
    {
    if(m->arr[m_y+1][m_x].m_symbol == ' ')
    {
        m->arr[m_y+1][m_x] = m->arr[m_y][m_x];
        m->arr[m_y][m_x] = CSpace(' ', m_x, m_y);
    }
    else if(m->arr[m_y+1][m_x].m_symbol == '#')
    {
        cout << "You cannot perform this move!"<<endl;
    }
    else if(m->arr[m_y+1][m_x].m_symbol == '$')
    {
        if(m->arr[m_y+2][m_x].m_symbol == ' ')
        {
            m->arr[m_y+2][m_x] = m->arr[m_y+1][m_x];
            m->arr[m_y+1][m_x] = m->arr[m_y][m_x];
            m->arr[m_y][m_x] = CSpace(' ', m_x, m_y);
        }
        else if(m->arr[m_y+2][m_x].m_symbol == '.')
        {
            m->arr[m_y+2][m_x] = m->arr[m_y+1][m_x];
            m->arr[m_y+2][m_x].m_symbol = '*';
            m->arr[m_y+1][m_x] = m->arr[m_y][m_x];
            m->arr[m_y][m_x] = CSpace(' ', m_x, m_y);
        }
        else if(m->arr[m_y+2][m_x].m_symbol == '$')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else if(m->arr[m_y+2][m_x].m_symbol == '#')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else if(m->arr[m_y+2][m_x].m_symbol == '*')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else
        {
            cout << "Unkown Error Occurred" << endl;
        }   

    }

    else if(m->arr[m_y+1][m_x].m_symbol == '.')
    {
        m->arr[m_y+1][m_x] = m->arr[m_y][m_x];
        m->arr[m_y][m_x] = CSpace(' ', m_x, m_y);
        m->arr[m_y+1][m_x].m_symbol = '+';
    }
    else if(m->arr[m_y+1][m_x].m_symbol == '*')
    {
        if(m->arr[m_y+2][m_x].m_symbol == ' ')
        {
            m->arr[m_y+2][m_x] = m->arr[m_y+1][m_x];
            m->arr[m_y+2][m_x].m_symbol = '$';
            m->arr[m_y+1][m_x] = m->arr[m_y][m_x];
            m->arr[m_y+1][m_x].m_symbol = '+';
            m->arr[m_y][m_x] = CSpace(' ', m_x, m_y);
        }
        else if(m->arr[m_y+2][m_x].m_symbol == '.')
        {
            m->arr[m_y+2][m_x] = m->arr[m_y+1][m_x];
            m->arr[m_y+1][m_x] = m->arr[m_y][m_x];
            m->arr[m_y+1][m_x].m_symbol = '+';
            m->arr[m_y][m_x] = CSpace(' ', m_x, m_y);
        }
        else if(m->arr[m_y+2][m_x].m_symbol == '$')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else if(m->arr[m_y+2][m_x].m_symbol == '#')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else if(m->arr[m_y+2][m_x].m_symbol == '*')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else
        {
            cout << "Unkown Error Occurred" << endl;
        }   
    }


    }

    void CPlayer::moveUp(CMaze maze)
    {
    if(maze.arr[m_y-1][m_x].m_symbol == ' ')
    {
        maze.arr[m_y-1][m_x] = maze.arr[m_y][m_x];
        maze.arr[m_y][m_x] = CSpace(' ', m_x, m_y);
    }
    else if(maze.arr[m_y-1][m_x].m_symbol == '#')
    {
        cout << "You cannot perform this move!"<<endl;
    }
    else if(maze.arr[m_y-1][m_x].m_symbol == '$')
    {
        if(maze.arr[m_y-2][m_x].m_symbol == ' ')
        {
            maze.arr[m_y-2][m_x] = maze.arr[m_y-1][m_x];
            maze.arr[m_y-1][m_x] = maze.arr[m_y][m_x];
            maze.arr[m_y][m_x] = CSpace(' ', m_x, m_y);
        }
        else if(maze.arr[m_y-2][m_x].m_symbol == '.')
        {
            maze.arr[m_y-2][m_x] = maze.arr[m_y-1][m_x];
            maze.arr[m_y-2][m_x].m_symbol = '*';
            maze.arr[m_y-1][m_x] = maze.arr[m_y][m_x];
            maze.arr[m_y][m_x] = CSpace(' ', m_x, m_y);
        }
        else if(maze.arr[m_y-2][m_x].m_symbol == '$')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else if(maze.arr[m_y-2][m_x].m_symbol == '#')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else if(maze.arr[m_y-2][m_x].m_symbol == '*')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else
        {
            cout << "Unkown Error Occurred" << endl;
        }   

    }

    else if(maze.arr[m_y-1][m_x].m_symbol == '.')
    {
        maze.arr[m_y-1][m_x] = maze.arr[m_y][m_x];
        maze.arr[m_y][m_x] = CSpace(' ', m_x, m_y);
        maze.arr[m_y-1][m_x].m_symbol = '+';
    }
    else if(maze.arr[m_y-1][m_x].m_symbol == '*')
    {
        if(maze.arr[m_y-2][m_x].m_symbol == ' ')
        {
            maze.arr[m_y-2][m_x] = maze.arr[m_y-1][m_x];
            maze.arr[m_y-2][m_x].m_symbol = '$';
            maze.arr[m_y-1][m_x] = maze.arr[m_y][m_x];
            maze.arr[m_y-1][m_x].m_symbol = '+';
            maze.arr[m_y][m_x] = CSpace(' ', m_x, m_y);
        }
        else if(maze.arr[m_y-2][m_x].m_symbol == '.')
        {
            maze.arr[m_y-2][m_x] = maze.arr[m_y-1][m_x];
            maze.arr[m_y-1][m_x] = maze.arr[m_y][m_x];
            maze.arr[m_y-1][m_x].m_symbol = '+';
            maze.arr[m_y][m_x] = CSpace(' ', m_x, m_y);
        }
        else if(maze.arr[m_y-2][m_x].m_symbol == '$')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else if(maze.arr[m_y-2][m_x].m_symbol == '#')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else if(maze.arr[m_y-2][m_x].m_symbol == '*')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else
        {
            cout << "Unkown Error Occurred" << endl;
        }   
    }
    }

    void CPlayer::moveLeft(CMaze maze)
    {
    if(maze.arr[m_y][m_x-1].m_symbol == ' ')
    {
        maze.arr[m_y][m_x-1] = maze.arr[m_y][m_x];
        maze.arr[m_y][m_x] = CSpace(' ', m_x, m_y);
    }
    else if(maze.arr[m_y][m_x-1].m_symbol == '#')
    {
        cout << "You cannot perform this move!"<<endl;
    }
    else if(maze.arr[m_y][m_x-1].m_symbol == '$')
    {
        if(maze.arr[m_y][m_x-2].m_symbol == ' ')
        {
            maze.arr[m_y][m_x-2] = maze.arr[m_y][m_x-1];
            maze.arr[m_y][m_x-1] = maze.arr[m_y][m_x];
            maze.arr[m_y][m_x] = CSpace(' ', m_x, m_y);
        }
        else if(maze.arr[m_y][m_x-1].m_symbol == '.')
        {
            maze.arr[m_y][m_x-2] = maze.arr[m_y][m_x-1];
            maze.arr[m_y][m_x-2].m_symbol = '*';
            maze.arr[m_y][m_x-1] = maze.arr[m_y][m_x];
            maze.arr[m_y][m_x] = CSpace(' ', m_x, m_y);
        }
        else if(maze.arr[m_y][m_x-2].m_symbol == '$')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else if(maze.arr[m_y][m_x-2].m_symbol == '#')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else if(maze.arr[m_y][m_x-2].m_symbol == '*')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else
        {
            cout << "Unkown Error Occurred" << endl;
        }   

    }

    else if(maze.arr[m_y][m_x-1].m_symbol == '.')
    {
        maze.arr[m_y][m_x-1] = maze.arr[m_y][m_x];
        maze.arr[m_y][m_x] = CSpace(' ', m_x, m_y);
        maze.arr[m_y][m_x-1].m_symbol = '+';
    }
    else if(maze.arr[m_y][m_x-1].m_symbol == '*')
    {
        if(maze.arr[m_y][m_x-2].m_symbol == ' ')
        {
            maze.arr[m_y][m_x-2] = maze.arr[m_y][m_x-1];
            maze.arr[m_y][m_x-2].m_symbol = '$';
            maze.arr[m_y][m_x-1] = maze.arr[m_y][m_x];
            maze.arr[m_y][m_x-1].m_symbol = '+';
            maze.arr[m_y][m_x] = CSpace(' ', m_x, m_y);
        }
        else if(maze.arr[m_y][m_x-2].m_symbol == '.')
        {
            maze.arr[m_y][m_x-2] = maze.arr[m_y][m_x-1];
            maze.arr[m_y][m_x-1] = maze.arr[m_y][m_x];
            maze.arr[m_y][m_x-1].m_symbol = '+';
            maze.arr[m_y][m_x] = CSpace(' ', m_x, m_y);
        }
        else if(maze.arr[m_y][m_x-2].m_symbol == '$')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else if(maze.arr[m_y][m_x-2].m_symbol == '#')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else if(maze.arr[m_y][m_x-2].m_symbol == '*')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else
        {
            cout << "Unkown Error Occurred" << endl;
        }   
    }
    }

    void CPlayer::moveRight(CMaze maze)
    {
    if(maze.arr[m_y][m_x+1].m_symbol == ' ')
    {
        maze.arr[m_y][m_x+1] = maze.arr[m_y][m_x];
        maze.arr[m_y][m_x] = CSpace(' ', m_x, m_y);
    }
    else if(maze.arr[m_y][m_x+1].m_symbol == '#')
    {
        cout << "You cannot perform this move!"<<endl;
    }
    else if(maze.arr[m_y][m_x+1].m_symbol == '$')
    {
        if(maze.arr[m_y][m_x+2].m_symbol == ' ')
        {
            maze.arr[m_y][m_x+2] = maze.arr[m_y][m_x+1];
            maze.arr[m_y][m_x+1] = maze.arr[m_y][m_x];
            maze.arr[m_y][m_x] = CSpace(' ', m_x, m_y);
        }
        else if(maze.arr[m_y][m_x+1].m_symbol == '.')
        {
            maze.arr[m_y][m_x+2] = maze.arr[m_y][m_x+1];
            maze.arr[m_y][m_x+2].m_symbol = '*';
            maze.arr[m_y][m_x+1] = maze.arr[m_y][m_x];
            maze.arr[m_y][m_x] = CSpace(' ', m_x, m_y);
        }
        else if(maze.arr[m_y][m_x+2].m_symbol == '$')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else if(maze.arr[m_y][m_x+2].m_symbol == '#')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else if(maze.arr[m_y][m_x+2].m_symbol == '*')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else
        {
            cout << "Unkown Error Occurred" << endl;
        }   

    }

    else if(maze.arr[m_y][m_x+1].m_symbol == '.')
    {
        maze.arr[m_y][m_x+1] = maze.arr[m_y][m_x];
        maze.arr[m_y][m_x] = CSpace(' ', m_x, m_y);
        maze.arr[m_y][m_x+1].m_symbol = '+';
    }
    else if(maze.arr[m_y][m_x+1].m_symbol == '*')
    {
        if(maze.arr[m_y][m_x+2].m_symbol == ' ')
        {
            maze.arr[m_y][m_x+2] = maze.arr[m_y][m_x+1];
            maze.arr[m_y][m_x+2].m_symbol = '$';
            maze.arr[m_y][m_x+1] = maze.arr[m_y][m_x];
            maze.arr[m_y][m_x+1].m_symbol = '+';
            maze.arr[m_y][m_x] = CSpace(' ', m_x, m_y);
        }
        else if(maze.arr[m_y][m_x+2].m_symbol == '.')
        {
            maze.arr[m_y][m_x+2] = maze.arr[m_y][m_x+1];
            maze.arr[m_y][m_x+1] = maze.arr[m_y][m_x];
            maze.arr[m_y][m_x+1].m_symbol = '+';
            maze.arr[m_y][m_x] = CSpace(' ', m_x, m_y);
        }
        else if(maze.arr[m_y][m_x+2].m_symbol == '$')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else if(maze.arr[m_y][m_x+2].m_symbol == '#')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else if(maze.arr[m_y][m_x+2].m_symbol == '*')
        {
            cout << "You cannot perform this move!" << endl;
        }
        else
        {
            cout << "Unkown Error Occurred" << endl;
        }   
    }
    }


    class CMaze
    {
public:
    friend class CPlayer;

    int m_columns, m_rows;
    string line;
    CBlocks arr[32][48];
    int i, j;
    int k, count;
    bool m_status;


    ifstream mazeStream;


    CMaze(void);
    ~CMaze(void);
    void setStatus(CMaze maze);
    CBlocks fillCharArr();
    void displayMaze();

    };
4

2 回答 2

0

Forward declaring CMaze and passing by reference will take care of c2061

class CMaze;
class CPlayer : public CBlocks
{
    public:
    CMaze * m;
    CPlayer(void);
    CPlayer(char sym, int x, int y, CMaze & mz);
    ~CPlayer(void);
    void moveUp(CMaze & maze);
    void moveDown(CMaze & maze);
    void moveLeft(CMaze & maze);
    void moveRight(CMaze & maze);
}:
于 2012-05-29T13:12:57.300 回答
0

我猜这与您没有包含来自 player.h 的迷宫标头这一事实有关...这意味着当您按值传递时(这很糟糕,因为它会导致不必要的副本)该函数尽管想使用它,但对 CMaze 对象一无所知。

我怀疑如果您将 CMaze 作为指针或引用传递,问题就会消失。但是,您确实需要记住,通过这样做,您函数内对 CMaze 的任何后续修改都会导致传入的 CMaze 也发生变化。

于 2012-05-29T13:17:55.513 回答