在数组中的简单代码中出现错误
error in simple code in in with array
我有一段代码
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();
}
相关文章:
- 将鼠标悬停在问题上时与预期">"相关的代码错误
- VI工作室代码错误无法打开输出文件主.exe
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- 有关矩阵的代码错误导致分段错误(内核转储)
- C++ Schannel POST 400 错误代码错误请求
- Java 本机访问代码错误:"Invalid memory access"
- 代码错误修改
- C++代码错误分配
- "_FCbuild"不能用作函数 - C/C++ 代码错误
- 我的代码错误类型警告 1 警告 C4018 和更多错误
- 代码错误(从 1 到 100 找到质数)
- OpenCV VisualStudio,C 代码错误尝试访问像素
- 当我使用的教程完全没有错误时,为什么我的代码错误
- 简单的NOOB C 输入/输出问题:代码错误
- 为什么enumProcessModules返回false值和299代码错误
- WinhttpSetoption()失败设置TLSV1.2,带有错误代码错误_internet_incorrect_ha
- 苹果安全传输代码错误
- C++ - 国际象棋主教移动代码错误
- 导入Python库时,嵌入了Python代码错误
- 编译提升C 代码错误