我在处理以下错误时遇到问题:
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();
};
答案 0 :(得分:0)
转发声明CMaze并通过引用传递将照顾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);
}:
答案 1 :(得分:0)
我猜这与你不包括来自player.h的迷宫头的事实有关...这意味着当你传递值时(这很糟糕,因为它会导致不必要的副本)尽管想要使用它,该函数对CMaze对象一无所知。
如果您将CMaze作为指针或引用传递,我怀疑问题会消失。但是,您需要记住,通过执行此操作,您的函数内的任何后续CMaze修改都将导致传入的CMaze也发生更改。